did_action() always returns zero

Support General did_action() always returns zero

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36232
    Alaan TVAlaan TV
    Participant

    I'm trying to use the did_action() function to avoid running my functions more than one time, like this:

    function create_my_fields( $meta_boxes ) {
    
        /*
         * Check if the action has been fired before or not.
         * The rest of the code will be run only on the first time that the action is triggered.
         */
        if ( did_action( 'rwmb_meta_boxes' ) !== 1 )
            return;
    
        $meta_boxes[] = [
            'title'        => '...',
            'id'           => '...',
            'post_types'   => '...',
            'context'      => '...',
            'fields'       => [
                ...
            ],
        ];
        return $meta_boxes;
    }
    add_filter( 'rwmb_meta_boxes', 'create_my_fields' );
    

    Here, I'm trying to avoid running the code more than one time, but the value of did_action( 'rwmb_meta_boxes' ) is always 0.

    This do_action function works fine with the native actions of WordPress, but it didn't work as expected with the rwmb_meta_boxes one. Is it something that you have to support? Or there's something wrong here?

    #36234
    Long NguyenLong Nguyen
    Moderator

    Hi,

    rwmb_meta_boxes is a filter hook, not an action hook as you can see on your code. If you want to check the action fired, please check it at the init hook, please read more here
    https://github.com/wpmetabox/meta-box/blob/master/inc/core.php#L23
    https://github.com/wpmetabox/meta-box/blob/master/inc/core.php#L48-L56

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.