Set page template programaticlly in plugin

admin2025-06-04  1

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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749046804a315900.html

最新回复(0)