Support » Developing with WordPress » How to create custom location for add_filter?

  • shaun21uk

    (@shaun21uk)


    Hi,

    I have a custom.php page in my WordPress plugin within which I want to output the following in a certain location:

    class Report_a_bug {
    
    public function __construct() {
    
        add_filter( 'the_content', array( $this, 'report_button' ), 10, 1 );
    
    }
    
    public function report_button( $content ) {
    
        $content .= '<div class="report-a-bug">
                        <button class="show-form" data-post_id="' . get_the_ID() . '">' . 
                            __( 'Report a bug', 'reportabug' ) . 
                        '</button>
                        <textarea class="report-a-bug-message" placeholder="' . __( 'Describe what\'s wrong...', 'reportabug' ) . '"></textarea>
                        <p class="report-a-bug-response"></p>
                    </div>';
    
        return $content;
    
    }
    
    }

    Instead of using ‘the_content’ how can I tell it to place the code in a custom location by using:

    new Report_a_bug();

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Unless your plugin is meant to work only on one specific site or theme, you are limited to where you can generate output. You need to depend on existing WP filters or actions, which are limited when it comes to page content. Who decides where a “custom” location is?

    What many plugins do to allow users to choose where they want plugin output is to provide both a template tag function and a shortcode (and/or editor block) users can use as they desire.

    shaun21uk

    (@shaun21uk)

    My custom hook is being used in a page within my plugin.

    Please can you tell me why this won’t work:

    class Report_a_bug
    {
    
        public function __construct()
        {
    
            $this->custom_hook();
            add_filter( 'custom_hook', array( $this, 'report_button' ), 10, 1 );
        }
    
        function custom_hook()
        {
            do_action('custom_hook');
        }
    
        public function report_button($content)
        {
            $content .= '<div class="report-a-bug">
            <button class="show-form" data-post_id="' . get_the_ID() . '">' .
                __('Report a bug', 'reportabug') .
                '</button>
            <textarea class="report-a-bug-message" placeholder="' . __('Describe what\'s wrong...', 'reportabug') . '"></textarea>
            <p class="report-a-bug-response"></p>
        </div>';
    
            return $content;
        }
    }

    Nothing shows when I call new Report_a_bug();

    I know the class is registered because I used a simple hello world output and that output fine.

    shaun21uk

    (@shaun21uk)

    I have also tried changing add_filter to add_action and still no output.

    Moderator bcworkz

    (@bcworkz)

    Try this:

        public function __construct()
        {
            add_filter( 'custom_hook', array( $this, 'report_button' ), 10, 1 );
            $this->custom_hook();
        }
    
        function custom_hook()
        {
            echo apply_filters('custom_hook');
        }

    The rest looks OK.

    shaun21uk

    (@shaun21uk)

    That’s interesting.

    I actually got it working about 30 minutes ago using add_action.

    I was missing new My_Loyalty_Stamp(); from the bottom of the class, which was a facepalm!

    I will still try your code to see if it works also.

    Thanks man!

    Moderator bcworkz

    (@bcworkz)

    Heh, been there done that. Actions and filters are pretty much handled by the same process internally. The distinction between the two are more for clarity of intent than anything else. If your callback is returning something you collect from the return of apply_filters(), then you have a filter. If you don’t collect any value, then it’s an action.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to create custom location for add_filter?’ is closed to new replies.