如何在wordpress后端和前端中隐藏页面

如何在wordpress后端和前端中隐藏页面

本文介绍了如何在wordpress后端和前端中隐藏页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的插件中,我创建了一个自定义模板,用于打印请求的侧边栏.为了运行这个模板的代码,我为它分配了一个自定义页面(通过调用 update_metadata).
将特定侧边栏的内容放入 Ajax 调用中是个好主意吗?
现在我的问题是 WORDPRESS 将它显示在仪表板和首页中,经过搜索,我没有找到任何易于理解的完全隐藏页面的解决方案,因此只能通过其 id 访问.
谁能告诉我怎么做?

In my plugin i have created a custom template that prints a requested sidebar. and for running the code of this template i assigned a custom page to it (by calling update_metadata) .
Is it a good idea for getting content of a specific sidebar into Ajax call ?
Now my problem is that WORDPRESS shows it in the dashboard and front page , and after searching i have not found any easy to understand solution for Hiding a page completely so only can be accessed by its id .
Can any one tell me how to do that ?

推荐答案

你的做法是错误的.您可以创建一个函数,该函数可以创建可以在 wordpress 页面上创建的任何内容.

you are going about this the wrong way. You can create a function that can create anything that can be created on a wordpress page.

但如果你真的必须,你可以在数据库之外创建一个页面,等等:

But if you really must you can create a page outside of the database, etc:

add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
   // add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
   // I created a custom post type for this plugin called market -- replace post_type with whatever you want
   //basically tell wordress to add a query var if sidebar is added to url.
   add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=market','top');
}

// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
    array_push($vars, 'is_sidebar_page');
    return $vars;
}

// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
    if(get_query_var('is_sidebar_page')){
       $new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
    if(file_exists($new_template))
        $template = $new_template;
    }
return $template;
}

这不会是管理部分或与页面相关的任何查询中的页面,但当然有人可以导航到此页面.但是正如我上面所说,您最好创建一个函数来创建侧边栏.如果你想要一个单独的文件来处理视图",你可以使用 require_once 'filename';一个文件,并使您的功能区域没有 html.

This will not be a page that will be in the admin section or in any query that relates to pages but someone could of course navigate to this page. But as i said above you would be better to create a function to create your sidebar. If you want a seperate file to handle the "view" you use require_once 'filename'; a file and keep your functions area free of html.

如果您在 wordpress 插件中创建函数,请不要忘记许多函数可能直到加载过程的后期才可用.如果遇到任何未定义的函数,请使用 add_action()

If you are creating functions in a wordpress plugin dont forget many functions may not be available until later in the load process. Use add_action() if you run into any undefined functions

您在进入模板之前正在加载 wordpress,因此您拥有所有功能.(google wp load 了解更多信息)+ get_header()/get_footer() 也会加载一些东西,比如 css 等.我在上面的代码中有一个小错字,修复了这个问题,但基本上你正在做的是告诉 wordpress 是否有人登陆 www.example.com/sidebar 以应用 query_var(重写规则).Wordpress 将查找其保存的变量(最终函数)并返回模板关联.第二个函数只注册 var.

you are loading wordpress before you get to the template so you have all the functions. (google wp load for more info) + get_header() / get_footer() will also load a few things like css, etc. I had a small typo in the code above, fixed that but basically what you are doing is telling wordpress if someone lands on www.example.com/sidebar to apply a query_var (rewrite rule). Wordpress will look up its saved vars (final function) and return the template assoc. The 2nd function just registers the var.

您在创建并包含在插件中的任何文件中也有 wp_functions,因此为什么您可以创建一个与此页面完全相同的文件.

You also have wp_functions in any file you create and include in a plugin, etc hence why you can create a file that does exactly the same as this page.

这篇关于如何在wordpress后端和前端中隐藏页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:52