Tag Archives: wordpress

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 […]

wordpress: how to retrieve only categories child of a given one

Filtering the list of categories is pretty easy. To retrieve only categories child of a specific one, proceed as follow: log into wordpress go to ‘categories’ click on ‘edit’ for the category you consider as top one in the url, check the value of “tag_ID“, this is your category id (22 in this example) in your code, where you are making a call to “wp_list_categories()” simply add the “child_of” argument as follow: wp_list_categories(“child_of=$your_top_category_id”); in this example: wp_list_categories(‘child_id=22’); that’s it! Additional customization is possible since a bunch of configuration variables (including category nesting, number of posts in a category, …) are […]

wordpress: how to filter which posts are to be displayed

Filtering the list of posts to be displayed, for example to create ‘sub-homepages’ dedicated to a given topic (ie. filtering on a given category), is pretty easy in wordpress. Full instructions are available here: http://codex.wordpress.org/The_Loop To make a long story short: posts to be displayed are retrieved through “have_posts();” function call upon which you loop to retrieve posts one by one to create a filter, simply call “query_posts($my_filter_string);” before calling “have_posts();”, by now posts have been filtered and have_posts() will return only posts matching your criteria all the incredibly smart aspect of this approach lies in $my_filter_string. This is simply […]

wordpress: how to set a particular template for a page

In wordpress documentation, it is said that you can set a particular template to be used for a particular page. That way, you can create your own pages of blog previews, like the home page, but for custom categories, as is done in this blog or you can create pages not made of posts at all. Key concept is flexibility. Guess what, this is true! Problem is that I did not find the option were it was supposed to be. In the doc I read, instructions were: WordPress can be configured to use different Page Templates for different Pages. Toward […]