0

I made a plugin using a custom post type, then I added a shortcode to display a specific post of that type in any page the user wants to. Now I want to put a meta box in the editor page so the user can just copy the shortcode without having to figure how he should insert it.

The idea is to put a disabled input with the shortcode for that post, and it will have a variable name as the post slug. I followed some tutorials but I couldn't make it work, the box is displayed but there is no content. Also I want that box to appear only on the custom post type editor page. Can you help me please?

Here is the code:

<?php
function slider_post_types_register() {
    register_post_type( 'slider',
        array(
            'labels' => array(
                'name' => __( 'Sliders' ),
                'singular_name' => __( 'Slider' ),
                'add_new' => __( 'Add New Slider' ),
                'add_new_item' => __( 'Add New Slider' ),
                'edit' => __( 'Edit' ),
                'edit_item' => __( 'Edit Slider' ),
                'new_item' => __( 'New Slider' ),
                'view' => __( 'View Slider' ),
                'view_item' => __( 'View Slider' ),
                'search_items' => __( 'Search Sliders' ),
                'not_found' => __( 'No Sliders' ),
                'not_found_in_trash' => __( 'No Sliders in the Trash' ),
            ),
            'hierarchical' => false,
            'public' => true,
            'menu_position' => 25,
            'menu_icon' => 'dashicons-images-alt',
            'has_archive' => 'sliders',
            'supports' => array( 'title', 'editor', 'thumbnail'),
            'description' => "Easy, simple sliders."
        )
    );
}
add_action( 'init', 'slider_post_types_register' );
// more code...
// register shortcode
function sw_slider_shortcode($atts) {
    $atts = shortcode_atts( array(
        'name' => '',
    ), $atts, 'sw_slider' );
    $name = $atts['name'];
    $output = '<section class="sw-slider">';
    $output .= display_slider($name);
    $output .= '</section>';
    return $output;
}
add_shortcode( 'sw_slider', 'sw_slider_shortcode');
/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'sw_slider_meta_boxes_setup' );
add_action( 'load-post-new.php', 'sw_slider_meta_boxes_setup' );
/* Meta box setup function. */
function sw_slider_meta_boxes_setup() {
  /* Add meta boxes on the 'add_meta_boxes' hook. */
  add_action( 'add_meta_boxes', 'sw_slider_metaboxes' );
}
function sw_slider_metaboxes()
{
    add_meta_box( 
        'sw_shortcode_box', 
        esc_html__( 'Shortcode for this slider' ), 
        'display_shortcode', 
        'slider', 
        'side', 
        'high' 
    );
}
function get_shortcode($name) {
    return '[sw_slider name="'. $name . '"]';
}
function display_shortcode($post) {
    $shortcode = get_shortcode($post->post_name);
    $output  = '<p><label for="sw_shortcode">Shortcode:</label><br>';
    $output .= '<input type="text" name="sw_shortcode" disabled="disabled" id="sw_shortcode" value="';
    $output .= $shortcode;
    $output .= '" /></p>';
}
?>
  • Please paste the essential parts of your code here. – Max Yudin Oct 11 '16 at 16:17
  • That's in the gist. I'm putting it here anyway. – Julián Bonilla Oct 11 '16 at 16:19
  • In the function display_shortcode, you must return $output – Benoti Oct 11 '16 at 17:11
  • In addition to @Benoti comment, you have to save the new post to make $post object available for display_shortcode() function. – Max Yudin Oct 11 '16 at 17:16
  • @Benoti, most likely to echo rather to return. – Max Yudin Oct 11 '16 at 17:19

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.