Passing arguments into ‘init’ function

The one thing you are missing is the use of use (http://php.net/manual/en/functions.anonymous.php example #3) You code will look something like //define post type name $pt_name=”post”; //remove default wysiwyg editor add_action(‘init’, function() use ($pt_name) { $post_type = $pt_name; remove_post_type_support( $post_type, ‘editor’); }, 100); This will let PHP know what is the currect scope to use when … Read more

add_action customize_register not working

You always need to be sure that three things are defined (section, setting and control). If you are adding a control to an already defined section, i.e. title_tagline, then you don’t need to re-register it, but always define the setting and the control. //adding setting for copyright text add_action(‘customize_register’, ‘theme_copyright_customizer’); function theme_copyright_customizer($wp_customize) { //adding section … Read more

Why, Where, and When to use reference pointers in filters/hooks?

The example you give is used when you’re building a plugin/theme using a class. In normal use, your functions.php file would just have: function my_function_to_filter( $args ) { // … function stuff here return $args; } add_filter(‘some_wp_filter’, ‘my_function_to_filter’); If you’re using a class, though, things would look different. You’d likely have a my-class.php file containing: … Read more

When and Where to use wp_insert_post()

Use some type of conditional tag to check if those posts exist or not. If they do not exist, have them created with wp_insert_post. Do this logic inside the AddMyPages function and not around the add_action function. Example You want to add a page with a parent ID only if it does not exist, and … Read more

How can I remove the WP menu from the admin bar?

This menu is added in WP_Admin_Bar::add_menus() with an action: add_action( ‘admin_bar_menu’, ‘wp_admin_bar_wp_menu’, 10 ); To remove it take the same action – just one step later. The following code works as a mu plugin or as a regular plugin: <?php # -*- coding: utf-8 -*- /** * Plugin Name: Remove WP Menu From Tool Bar … Read more

Add something to beginning of the content

the_content is also a filter, into which the content is passed as an argument. You simply prepend your content and then return like so. add_filter(‘the_content’,’prepend_this’); function prepend_this($content) { $content = “string to prepend” . $content; return $content }

Cron jobs in a class

I was working with wp_cron last week in a plugin and we have a fight and are no longer on speaking terms, but for reference this is what I do; 1) – set the scheduled cron event on register_activation_hook 2) – remove the scheduled cron event on register_deactivation_hook If you are concerned that your scheduled … Read more

Difference between do_action(‘admin_enqueue_scripts’, $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax

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 ) { switch ( $hook_suffix ) { case ‘index.php’: // dashboard // do one … Read more

Unable to prevent function using save_post firing twice

First, you can use this hook to target only one custom type: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/ This hook (and save_post) is called the first time when you click on “new …” and then the hook is called with $update = FALSE. Then to send e-mail only when the object is updated, you can test $update like this: const … Read more