0

I am creating a page within a plugin, I am then trying to add a post state to this page however I seem to only be able to add the state to all pages as the filter is called before the create page function.

Inside my plugins index class file:

class Store_Locator {

/**
 * Init function to run on plugin load
 */
public static function init() {
    $class = __CLASS__;
    new $class;
}

public function __construct() {

    //Lots of actions which I've removed for easier display
    add_filter( 'display_post_states',  array($this,'add_display_post_states'));

    return $this;

}

public function create_locator_page() {
    // Create post object
    $create_post = array(
        'post_title'    => wp_strip_all_tags( 'Store Locator' ),
        'post_slug'     => wp_strip_all_tags( 'store-locator' ),
        'post_content'  => '',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_type'     => 'page',
    );

    // Insert the post into the database
    $postId = wp_insert_post( $create_post );

    Store_Locator::add_display_post_states($postId);
}

public function add_display_post_states( $postId ) {

    global $post;

    if( 'page' == get_post_type($postId) && $postId = $post->ID) {
        $states['store-locator'] = __('Store Locator', 'custom-store-locator');
    }

    return $states;
}

How do I get the filter to run only once the page has been created so that it assigns only to the selected post that I wish to pass to it?

Your Answer

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

Browse other questions tagged or ask your own question.