0

I'd like users of my theme to be able to select a single post template via a dropdown menu on the post screen.

I know of a plugin that achieves this but I would like to be able to achieve the same thing without the plugin. Anybody have a solution?

1
  • 2
    What's wrong with the plugin? It consists of 142 lines of code, seems pretty well coded and does the job. One could simply paste it here and say: "this works". What kind of different solution are you looking for? I can imagine one that would involve a Custom Meta Box, a dropdown with hard-coded template names, and then in single.php do a include("$custom_field.php");. And at the end, it would be a brand new... plugin.
    – brasofilo
    Apr 12, 2013 at 15:30

1 Answer 1

0

`

        if (is_singular()) {
            $zifana_captain_hook = get_page_template();

            if ($zifana_captain_hook && !preg_match('/page.php$/', $zifana_captain_hook)) {
                include($zifana_captain_hook);
                die();
            }

        }

    }

    // -- Function Name : save_post
    // -- Params : $post_id, $post
    // -- Purpose : 
    function save_post($post_id, $post) {
        $zifana_captain_hook = isset($_POST['zifana_theme_styling'])? $_POST['zifana_theme_styling'] :
        '';

        if ( !empty($zifana_captain_hook) ) {
            $zifana_captain_hooks = get_page_templates();
            $post->page_template = $zifana_captain_hook;

            if ( 'default' != $zifana_captain_hook && !in_array($zifana_captain_hook, $zifana_captain_hooks) ) {
                return;
            }

            update_post_meta($post_id, '_wp_page_template', $zifana_captain_hook);
        }

    }

    // -- Function Name : zifana_hook_theme_metabox
    // -- Params : $post
    // -- Purpose : 
    function zifana_hook_theme_metabox($post) {
        $zifana_captain_hook = get_post_meta( $post->ID, '_wp_page_template', true );
        ?>

    // -- Function Name : add_dropdown
    // -- Params : 
    // -- Purpose : 
    function add_dropdown() {

        if ( 0 != count( get_page_templates() ) ) {
            add_meta_box('themezifanastyle',__( 'What template huh..', 'zifana'),array(&$this, 'zifana_hook_theme_metabox'),'post');
        }

    }

};
$__zifana_theme_stylings = & new zifanaTemplate;

if (is_admin()) {
    add_action('admin_menu', array(&$__zifana_theme_stylings, 'add_dropdown'));
}

add_action('zifana_page_hook', array(&$__zifana_theme_stylings, 'zifana_page_hook'));
add_action('save_post', array(&$__zifana_theme_stylings, 'save_post'), 10, 2);

` i hope helpfull.. :)

1
  • Please add some explanation to go with your code.
    – s_ha_dum
    Jul 19, 2013 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.