$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Set page template programaticlly in plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Set page template programaticlly in plugin

matteradmin9PV0评论

Task: I have created a page using my plugin and that page has to be set a certain page template within page creation. Everything works apart of setting page template. I have manually moved page template file into theme directory for now and setting page template manually however it's not good for me. Page template file located in the same directory with plugin main file.
Question: How to make my plugin to set page template? What do I missing?

Code to programmatically create page:

    <?php
function create_namnam_login_page($post_name) {
    global $wpdb;
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
        return true;
    } else {
        return false;
    }
}

$postarr = array(
        'post_author'   => 1,
        'post_title'    => 'NamNam Login',
        'post_status'   => 'publish',
        'post_type'     => 'page',
        'post_slug'     => 'namnam-login',
        'page_template' => 'namnam-login.php',
    );

if (the_slug_exists('namnam-login')) {
    return false;
}
else {
    $namnam_login = wp_insert_post( $postarr, $wp_error = false );
    update_option( 'namnamloginpage', $namnam_login ); 
}
?>

Plugin activation hook:

<?php
    function namnam_create_login_page() {

        include_once dirname( __FILE__ ) . '/admin-login.php';

        create_namnam_login_page($post_name);
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'namnam_create_login_page' );

    function namnam_delete_login_page() {
        $the_page_id = get_option('namnamloginpage');
        if( $the_page_id ) {
        wp_delete_post( $the_page_id, $force_delete = true );
        }

        flush_rewrite_rules();
    }
    register_deactivation_hook( __FILE__, 'namnam_delete_login_page' );
    ?>

Task: I have created a page using my plugin and that page has to be set a certain page template within page creation. Everything works apart of setting page template. I have manually moved page template file into theme directory for now and setting page template manually however it's not good for me. Page template file located in the same directory with plugin main file.
Question: How to make my plugin to set page template? What do I missing?

Code to programmatically create page:

    <?php
function create_namnam_login_page($post_name) {
    global $wpdb;
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
        return true;
    } else {
        return false;
    }
}

$postarr = array(
        'post_author'   => 1,
        'post_title'    => 'NamNam Login',
        'post_status'   => 'publish',
        'post_type'     => 'page',
        'post_slug'     => 'namnam-login',
        'page_template' => 'namnam-login.php',
    );

if (the_slug_exists('namnam-login')) {
    return false;
}
else {
    $namnam_login = wp_insert_post( $postarr, $wp_error = false );
    update_option( 'namnamloginpage', $namnam_login ); 
}
?>

Plugin activation hook:

<?php
    function namnam_create_login_page() {

        include_once dirname( __FILE__ ) . '/admin-login.php';

        create_namnam_login_page($post_name);
        flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'namnam_create_login_page' );

    function namnam_delete_login_page() {
        $the_page_id = get_option('namnamloginpage');
        if( $the_page_id ) {
        wp_delete_post( $the_page_id, $force_delete = true );
        }

        flush_rewrite_rules();
    }
    register_deactivation_hook( __FILE__, 'namnam_delete_login_page' );
    ?>
Share Improve this question asked Jan 4, 2019 at 2:43 PrusakovPrusakov 1255 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can use the page_template filter to load a page template from the plugin directory:

function wpd_plugin_page_template( $page_template ){
    if ( is_page( 'namnam-login' ) ) {
        $page_template = dirname( __FILE__ ) . '/namnam-login.php';
    }
    return $page_template;
}
add_filter( 'page_template', 'wpd_plugin_page_template' );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far