Home > Enterprise >  Wordpress side bar admin menu for my custom post type
Wordpress side bar admin menu for my custom post type

Time:12-01

I have created a custom post type called "Home Page" using register_post_type and below is my code.

$labels = array(
             
        'name' => __( 'Home Page'),
        'singular_name' => __( 'Home Page'),
        'edit_item' => __( 'Edit Home Page'),            
    );

$args = array(
        'label'               => __( 'Home Page'),
        'description'         => __( 'Custom Post Type'),
        'labels'              => $labels,       
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => false,        
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'map_meta_cap'        => fa,
        'capabilities' => array(
            'create_posts' => 'do_not_allow',
            'delete_published_posts' => false,
            'read_post' => false,
        ),
        
        
    );
register_post_type( 'homepage', $args );

And I have used "Advanced Custom Fields" WordPress plugin to add custom fields to this new custom post type. These all working fine and its showing like below.

enter image description here

But the thing I want is, I will only have 1 record so if I click on "Home Page" on the left side bar admin menu link, it should be redirected to that particular record with something like post.php?post=37&action=edit in the URL.

I already know doing 'show_in_menu' => false, will hide the url from the left sidebar.

But how can I hide this menu and directly go to edit page for only single record which I will always have?

Can someone guide me what should I do from here on to achieve this?

Thanks in advance.

CodePudding user response:

if you have already made the first post:

in your register_post_type add these parameters:

'map_meta_cap' => true,
'capabilities' => array(
    'create_posts' => 'do_not_allow'
)

this will disable adding new posts to the post type.

And then the function to redirect the edit.php - making sure you pass the correct ID

function redirect_edit_screen() {
    
    $current_screen = get_current_screen();
    
    if( $current_screen->id === "edit-homepage" ) {
        wp_safe_redirect( admin_url('post.php?post=37&action=edit') );
        exit();
    }
}
add_action( 'current_screen', 'redirect_edit_screen' );

CodePudding user response:

OK Guys,

Eventually, I have found a way to implement this feature. Here is what I have done.

// Adding custom menu for custom post type 
function add_link_to_category_tips_n_tricks() {
    $link = 'post.php?post=39&action=edit';
    add_menu_page( 'Home Page', 'Home Page', 'edit_pages', $link, '', 'dashicons-admin-home', 8 );
}
add_action( 'admin_menu', 'add_link_to_category_tips_n_tricks' );

This will create a new custom menu and will redirect at the page where I want.

Thanks all for your support and guidance.

CodePudding user response:

One way of doing it will be to redirect on admin_init hook. Check if $_GET[post_type'] is set and equal to your custom post type and the $pagenow global is edit.php. The nsimply redirect it using wp_redirect().

function redirect_to_edit() {

    global $pagenow;

    if ( isset($_GET['post_type']) && $_GET['post_type'] == 'homepage' && $pagenow == 'edit.php' ) {
      wp_redirect( admin_url('post.php?post=39&action=edit') );
      exit();
    }

}
add_action('admin_init', 'redirect_to_edit');

I would prefer the way to create a new custom menu and redirect it to the page needed.

  • Related