Category Archives: wordpress

steps to change wordpress db table prefixes after installation

In below tutorial, we will see how to change wordpress db table prefix from ‘wp_’ into ‘wp2_’ without breaking wordpress. Adapt below content to match both your source and target table prefixes. Here are the steps to change wordpress db table prefixes without breaking the websites: backup your current db (=> export it into a file stored somewhere safe and accessible) update all option names starting with old prefix in table named ‘wp_options’. Entries can be identified by running query: select * from wp_options where option_name like ‘wp_%’ Update field ‘option_name’ for all the entries, one by one. update all […]

wordpress: prevent direct access to file from outside the workflow

Let’s say you don’t want users to access your script file via typing it’s url in the browser, here is a simple way for instance to make sure wordpress framework has been loaded and section is admin section: if (!defined(‘WP_ADMIN’)) { die(‘direct access not permitted’); }

wp automatically escaping global vars!

This thing is incredible. Php has been telling people for ages that variables should not be automatically escaped, that it was an error to the extent they even removed this feature altogether starting from php 5.4. Guess what? WordPress has it’s own behavior to escape the global vars on its own! independently from php! And even better, they know it for more than a year and there is no native way to deactivate this! Before wp is loaded: var_dump($_GET[‘var’]); //string ‘st laurent d” (length=13) Single quote at end of string is unescaped. After wp is loaded: var_dump($_GET[‘var’]); //string ‘st laurent […]

wordpress: add css file for your plugin admin page only

If you want to add a css file that is loaded only when your plugin admin page is displayed, here is how to do it: Step 1: attach to appropriate hook: add_action(‘admin_enqueue_scripts’, ‘myplugin_loadAdminCss’); Step 2: inside your hooked function, filter on provided parameter: function myplugin_loadAdminCss($hook) {         if ((‘plugins_page_XXX’) != $hook) { return; } wp_register_style(‘myplugin_admin_css’, plugins_url(‘/css/admin.css’, __FILE__), false, ‘1.0.0); } Key here is to filter on value provided to your function (named $hook here). This value corresponds to the admin page being currently displayed, with a special value when this page is the “plugins.php” page where $hook is ‘plugins_page_XXX’ […]

The plugin generated XXX characters of unexpected output during activation [SOLVED]

While writing my own wordpress plugin, I ran into the following error at plugin activation time: The plugin generated 145582 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin. Step 1: Capture the unexpected output WordPress error message is not that useful because we have no clue on what the unexpected output is.To find what generated this output, we need first to know what this output is. The trick here is to add an action to ‘activated_plugin’ hook which will simply dump the […]

wordpress plugin admin pages – infinite loop on login page

While writing a wordpress plugin with several admin pages (at least pages for identified users) I ran into the following issue: Upon every successful login, my page content was not displayed and user was redirected to login screen in what seemed to be a never ending loop. After lengthy debugging, reason for this was that after login, I redirected my user to a custom short url which did not start with ‘wp-admin/xxxx’. Then in my page being loaded, when I included the file /wp-admin/admin.php to load wp framework, this redirected me to login page. When a user logs in using […]

wp: flush_rewrite_rules – rewriting rules with wordpress

Here is how rewriting rules with wordpress works: Step 1 Add your url-rewriting rules using add_rewrite_rule($pattern, $replace, ‘top’) function Step 2 Flush your rules This is the tricky part. After having added your rules at step1, rules will not be active until you call flush_rewrite_rules(). Even when doing this, things might still not work. Reason is that wp handles url-rewriting in two different ways based on the target url being used (where target url means the url matching the pretty url). Case 1: target url is of the form ‘index.php?xxx’ In that case, redirection is done at php level. Calling […]