0

I'm trying to create my own class to make some of the most common changes I need in all my WP sites. Things like removing the canonical tag when not required or changing the meta description.

I'm not creating a plugin, as I'm not familiar with it.

I have written the following class, but I've been not able to get it fully working. As soon as _setup function has been called, anything else is outputted on the source code. Similarly all the add_action('wp_head') are ignored.

I suspect that this is because I am recalling the class too late, but I don't know whether this is the case and how I can test it.

    class Stuff_Frontend {
    private static $_instance = null;

    public static function init()
    {
        add_action( 'wp_head', array(self::instance(), '_setup'));
    }

    public static function instance()
    {
        // create a new object if it doesn't exist.
        is_null(self::$_instance) && self::$_instance = new self;
        return self::$_instance;
    }

    /**
     * Setup the class
     * Adds and removes a lot of filters.
     */
    function _setup() {
        echo "I'm here;";
        add_action( 'wp_head', array(self::instance(), 'head' ), 1 );

        // Remove actions that we will handle through our wpseo_head call, and probably change the output of
        remove_action( 'wp_head', 'rel_canonical' );
        remove_action( 'wp_head', 'index_rel_link' );
        remove_action( 'wp_head', 'start_post_rel_link' );
        remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
        remove_action( 'wp_head', 'rsd_link' );
        remove_action( 'wp_head', 'wlwmanifest_link' );

        add_action( 'mystuff', array(self::instance(), 'change_metadesc' ), 10 );
    }

    /**
     * Main wrapper function attached to wp_head. This combines all the output on the frontend.
     */
    public function head() {
        echo "this is a test for my class head";

        /**
         * Action: 'mystuff'
         */
        do_action( 'mystuff' );

        return;
    }   

    /**
     * Change the meta description element.
     *
     * @param bool $echo Whether or not to echo the description.
     *
     * @return string
     */
    public function change_metadesc() {

        echo "this is an attempt to change the description";
    }
    }        

The class above is recalled in the function.php ini using the following statements:

include( locate_template( 'MyStuff.php' ) );
Stuff_Frontend::init();

Any hint is appreciated. Thanks

0

You add your _setup function to wp_head without priority, so it's given priority of 20. All of the actions you are trying to remove within that function either have a higher priority (lower number), or same priority but are already in the queue ahead of yours, so when your _setup function runs, they've already sent their output. There's no reason to wait until wp_head to remove those actions, you can do it on the init action.

Also FYI, all your file needs is a plugin header and it'll be a stand-alone plugin.

| improve this answer | |
  • But I have the priority set in the _setup function for the wp_head call in the _setup funciont. Adding a priority for the _setup call in the init resulted only in my "I'm here" echo message appear just a little above, but all the other echo messages are still not on the screen. – Andrea Moro Aug 5 '14 at 21:27
  • BTW by moving the remove_actions in the init does work, but I don't need to set a priority. If I do set it to 1, they don't work. I suspect that also removing bit in WP requires a knowledge of when something is executed? – Andrea Moro Aug 5 '14 at 21:36
  • inside init() function you add your setup function with no priority, so it runs on priority 20. in _setup(), you add something to wp_head with priority 1, but that already happened before your function executed, because you're now at priority 20. I wasn't suggesting you add a priority to the remove actions, all that matters is when your code that calls those remove actions executes, you're trying to remove things after they've already happened. – Milo Aug 5 '14 at 21:51
  • I see .. thanks for pointing me out on the mistake. – Andrea Moro Aug 5 '14 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.