doulaozhang0238 2016-07-07 07:30
浏览 79

使用自定义插件wordpress将页面模板添加到自定义页面

I am new to developing the wordpress plugin. I am developing a plugin and I have succeeded in creating a new page whenever the plugin is activated but I want to add a custom php template file to that page. Thats means on activation I want to create a custom page with a custom template. Is it possible to do?

To create a new page I have used this code but could not find anything on adding the template thing:

register_activation_hook(__FILE__,'my_plugin_install'); 
function my_plugin_install() {
    global $wpdb;
    $the_page_title = 'Custom Cart';
    $the_page_name = 'cart';
    delete_option("Custom Cart");
    add_option("Custom Cart", $the_page_title, '', 'yes');
    delete_option("cart");
    add_option("cart", $the_page_name, '', 'yes');
    delete_option("my_plugin_page_id");
    add_option("my_plugin_page_id", '0', '', 'yes');
    $the_page = get_page_by_title( $the_page_title );
    if ( ! $the_page ) {
        $_p = array();
        $_p['post_title'] = $the_page_title;
        $_p['post_content'] = "This text may be overridden by the plugin. You shouldn't edit it.";
        $_p['post_status'] = 'publish';
        $_p['post_type'] = 'page';
        $_p['comment_status'] = 'closed';
        $_p['ping_status'] = 'closed';
        $_p['post_category'] = array(1);
        $the_page_id = wp_insert_post( $_p );
    }
    else {
        $the_page_id = $the_page->ID;
        $the_page->post_status = 'publish';
        $the_page_id = wp_update_post( $the_page );
    }
    delete_option( 'my_plugin_page_id' );
    add_option( 'my_plugin_page_id', $the_page_id );
}
  • 写回答

1条回答 默认 最新

  • duanbishai5271 2016-07-07 13:12
    关注

    It's a little bit tricky. But we have article for you. It can be done. Just try http://www.wpexplorer.com/wordpress-page-templates-plugin/

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算