Tag Info

Hot answers tagged

15

When you click 'New Post', you're simply loading the page wp-admin/post-new.php. In doing so, WordPress will always create a new post (an 'Auto Draft') to ensure all other features (such as media uploads) and plugins work as normal, even before you actually save a draft or publish the post. And this, in turn, triggers save_post. Hence your echo. Okay, ...


13

Whenever a plugin creates a new MyClass();, it should assign it to a uniquely named variable. That way, the instance of the class is accessible. So if he was doing $myclass = new MyClass();, then you could do this: global $myclass; remove_action( 'wp_footer', array( $myclass, 'my_action' ) ); This works because plugins are included in the global ...


13

By default this is not possible. There are workarounds if you do it the OOP way. You could create a class to store the values you want to use later. Example: /** * Stores a value and calls any existing function with this value. */ class WPSE_Filter_Storage { /** * Filled by __construct(). Used by __call(). * * @type mixed Any type you ...


11

after_setup_theme action hook is fired before the actual $wp->init(); which Set up the current user and only then init action hook is fired which means that they are pretty much the same with one major difference and that is at after_setup_theme the user is not authenticated, and at init he is (assuming that we are talking about an actual user).


11

You can do it like this (put in your functions.php) : function add_admin_scripts( $hook ) { global $post; if ( $hook == 'post-new.php' || $hook == 'post.php' ) { if ( 'recipes' === $post->post_type ) { wp_enqueue_script( 'myscript', get_stylesheet_directory_uri().'/js/myscript.js' ); } } } add_action( ...


10

You can use the remove_action() function, like this: remove_action('publish_post', 'old_action'); add_action('publish_post', 'new_action'); It's important to note that if the old_action was added with a priority parameter, you must add that to the remove_action call, otherwise it will fail to remove it. There are other implications if the old_action was ...


8

deregister doesn't work for you because WP concatenates scripts in admin area by default. So when you make jQuery load from elsewhere it falls apart. You can disable concatenation to make it work (add conditionals as needed): add_action( 'admin_init', 'jquery_admin' ); function jquery_admin() { global $concatenate_scripts; $concatenate_scripts = ...


8

For … do_action('admin_enqueue_scripts', $hook_suffix); … the callback gets the hook suffix as first parameter. Since it is an action you don't have to return anything. You can use it in a rather flexible callback function: add_action('admin_enqueue_scripts', 'wpse_49993_admin_script_callback' ); function wpse_49993_admin_script_callback( $hook_suffix ) ...


7

I prefer hooks, since they are more flexible: you can hook into them from your theme's functions.php file, but also from plugins. I try to put as much logic in plugins, so that the themes contain mostly layout stuff. If you use an action hook, it is still possible to use get_template_part() in that hook handler. This gives you the best of both worlds. You ...


7

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin. /** * Search SQL filter for matching against post title only. */ function __search_by_title_only( $search, &$wp_query ) { global $wpdb; if ( empty( $search ) ) return $search; // skip processing - no search term in query $q = ...


7

My favorite plugin for that is Core Control which has very nice module for display of what is going in the cron - which events are set up, when are they next firing, etc. On getting your hands dirty level see _get_cron_array(), which returns internal stored data for cron events (top level of keys are timestamps).


7

The action hook wp_login runs when the user logs in - it can run a simple function. function do_anything() { //do stuff } add_action('wp_login', 'do_anything'); The real breadwinner here is wp_authenticate which has a bit of documentation. It passes an array with the given username and password, which gives you the opportunity to pass info to the ...


7

Try this... add_action('post_updated', 'myfunction'); function myfunction( $post_id ) { global $post; if (!file_exists("/www/foo/blog/wp-content/uploads/" . $post_id)) { mkdir("/www/foo/blog/wp-content/uploads/" . $post_id, 0777); } } NOTE: Change from save_posts to post_updated which will stop the duplicate issue as it ...


7

Ajax in WordPress works by sending an HTTP post to /wp-admin/admin-ajax.php (by default) that then fires the corresponding hook. So, you attach some jquery to an event triggered by your delete button, which then posts to admin-ajax.php, which has an action, say, delete_my_options(), which actually runs the php to delete. Then, you have a function, called a ...


6

I'd say template_redirect. But take a look at the Action Reference. Example Don't forget to exit() on redirect. /** * This example redirects everything to the index.php page * You can do the same for the dashboard with admin_url( '/' ); * Or simply base the redirect on conditionals like * is_*() functions, current_user_can( 'capability' ), globals, ...


6

When you "hook"/add_action/*_filter('whatever'); a callback function to do_action('whatever');, then you basically add the function (or object-method) name to the global $wp_filters-array. Doing so, you add the function/method name to an array that is built like the following $wp_filter[ $tag ][ $priority ][ $idx ] // $tag = action/filter name // $priority ...


6

Don't ask me way but i actually have a function to count hooked functions to a tag /** * count_hooked_functions * @author Ohad Raz * @param string $tag hook name as string * @return int the number of hooked functions to a specific hook */ function count_hooked_functions($tag = false){ global $wp_filter; if ($tag){ if ...


5

Via the "Annotate" function the the Trac, you can see that this was added three years ago, after the request for a generic POST handler that plugins can use. Google code search tells me that at least Akismet uses this hook, and it appeared there at the time it was introduced in core. It works by calling admin.php directly (and not the plugin page). From ...


5

For all admin pages and front end pages except individual post edit screens (wp-admin/post.php), 'wp' is the most reliable hook for getting the global values. http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-includes/class-wp.php.source.html#l486 You can see there that it fires immediately after WP::main() fires WP::register_globals(). The problem with ...


5

You can globalize $post to work out the post type. Example: add_filter( 'the_title', 'add_the_title_prefix' ); function add_the_title_prefix( $title ) { global $post; if ( 'custom_post_type_name' != $post->post_type ) return $title; return "<span>Press:</span> {$title}"; }


5

There are 3 methods. Somewhat weird but since this text is internationalized you can filter the output. This is just an example to remove the text, the link is still present in the source. add_filter('gettext', 'remove_powered_by', 20, 3); function remove_powered_by( $translated_text, $untranslated_text, $domain ) { $custom_field_text = 'Proudly ...


5

SOLUTION: after_switch_theme does exactly what I intended here. It fires after the theme is switched TO your theme. One of the solutions mentioned below uses switch_theme. This does not have the desired results, since it only happens upon switching away from your theme. Here is an article that I found as reference: ...


5

The best way to learn the most important filters is to read the source code. Whenever you want to change the output of a function read its source. Then you’ll either find the hook in this function or in another function called by the first. In some cases there is no hook and you have to hack … but that’s the next level. There are some tools to inspect ...


5

Use is_home instead, as is_page_template will not work for home.php as its technically not a page template in the traditional sense. add_action('template_redirect', 'are_we_home_yet', 10); function are_we_home_yet(){ if ( is_home() ) { //your logic here } } Revised: add_action('template_redirect', 'are_we_home_yet', 10); function ...


5

There are multiple actions. Listed in order of appearance with their parameters: untrash_post - (int) $post_id // before restoring transition_post_status - (string) $new_status, 'trash', (object) $post trash_to_{$new_status} - (object) $post // useful to address a special trash to status action untrash_post_comments - (int) $post_id // before associated ...


5

The action is update_option_permalink_structure. You get the old and the new value as parameters. add_action( 'update_option_permalink_structure' , 'my_custom_function', 10, 2 ); function my_custom_function( $oldvalue, $_newvalue ) { // do something } There are also the actions update_option_category_base and update_option_tag_base.


4

register_post_type() has a registration option called 'register_meta_box_cb'. Set that to a valid callback and it will call that function only when it is compiling the meta boxes for that post type's edit screen. Something like this: register_post_type( 'foo', array( 'public' => true, 'label' => 'foo', 'register_meta_box_cb' => 'bar', )); ...


4

The user roles and capabilities are saved in the database so once you have you have used add_role() its saved and then next load WordPress will know that role just like the built in roles. Now if you look at the function add_role() more specifically at line 141 you will see that it only saves the role and capabilities in the database if the var $use_db is ...


4

If the post content, title and excerpt are empty WordPress will prevent the insertion of the post. You can trick WordPress by first filtering the input array so empty values are set to something else, and then later resetting these values back to empty strings. This will bypass the standard check. add_filter('pre_post_title', 'wpse28021_mask_empty'); ...


4

The best thing to do here is to use a static class. The following code should be instructional: class MyClass { function __construct() { add_action( 'wp_footer', array( $this, 'my_action' ) ); } function my_action() { print '<h1>' . __class__ . ' - ' . __function__ . '</h1>'; } } new MyClass(); class ...



Only top voted, non community-wiki answers of a minimum length are eligible