Trouble understanding apply_filters()

Try to see the function with better names: apply_filters( $filter_name, // used for add_filter( $filter_name, ‘callback’ ); $value_to_change, // the only variable whose value you can change $context_1, // context $context_2 // more context ); So when that function is called as: // wp-login.php line 94 apply_filters( ‘login_body_class’, $classes, $action ); You can use … … Read more

How do you use a CPT as the default home page?

Thanks to @toscho for the useful answer, but it felt a bit hackish to me, so I poked around a bit and figured out I could add a filter instead: function wpa18013_add_pages_to_dropdown( $pages, $r ){ if(‘page_on_front’ == $r[‘name’]){ $args = array( ‘post_type’ => ‘stack’ ); $stacks = get_posts($args); $pages = array_merge($pages, $stacks); } return $pages; … Read more

How do filters and hooks really work in PHP

Overview Basically the “Plugin API,” which summons Filters and Hooks, consists out of the following functions: apply_filters() – execute do_action – execute apply_filters_ref_array() – execute do_action_ref_array() – execute add_filter() – add to stack add_action() – add to stack Basic Internals Overall there’re a couple of globals (what else in WordPress world) involved: global $wp_filter, $wp_actions, … Read more

Difference between do_action and add_action

Use do_action( ‘unique_name’ ) to create your own actions. You can use that to offer an API for your plugin, so other plugins can register callbacks for your custom action. Example: Do I need to call do_action in my plugin? But you can use custom actions (or filters) in a theme too. Example: Best practice … Read more

How to enqueue scripts on custom post add/edit pages?

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( ‘admin_enqueue_scripts’, ‘add_admin_scripts’, 10, 1 );

Hook for post and page load

You can use the wp hook and check the global $wp_query object or any conditional. add_action( ‘wp’, ‘wpse69369_special_thingy’ ); function wpse69369_special_thingy() { if ( ‘special_cpt’ === get_post_type() AND is_singular() ) return print “Yo World!”; return printf( ‘<p>Nothing to see here! Check the object!<br /></p><pre>%s</pre>’, var_export( $GLOBALS[‘wp_query’], true ) ); } See: wp in codex.wordpress.org and … Read more

how to limit search to post titles?

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. * * @link http://wordpress.stackexchange.com/a/11826/1685 * * @param string $search * @param WP_Query $wp_query */ function wpse_11826_search_by_title( $search, $wp_query ) { if ( ! empty( $search ) && ! … Read more