2

嗨,我对 wordpress 插件开发非常陌生,我创建了一个插件,它添加了一个 Sidemenu 来显示 jquery 对话框,

现在我试图在页面中向前端用户显示一个表单,但我很困惑如何开始,

我想在前端用户的侧面菜单中显示一个链接,当用户单击它时,我想用插件打开我的自定义页面。(点击后网址应该是这样的:demo.com/my_page.php)。我用谷歌搜索了它,但没有发现任何理解一些建议模板的东西(我还不知道它们)。

请你建议我最好的方法来做到这一点。

4

3 回答 3

3

PHP 版本 5+

对于我将在下面描述的前两种方法,首先要做的是在 Wordpress 中创建页面。选择页面-> 新建以创建它,标题为“我的页面”或“我的页面”。在这两种情况下,slug 都应该是“我的页面”,除非已经存在。不要使用“my_page”。

有几种方法可以在插件中管理前端页面。

我最喜欢的一种是将页面放在插件的目录中,然后将其复制到主题的目录中,如下例所示。

像往常一样创建表单并将其保存在插件目录中。

This file will be the template for the page in the stylesheet directory. The page must be located in this directory, as far as I know, for WP to load it.

Example:

// Make sure you define in your plugin's main script the location of the plugin, with a code like this:

// Holds the absolute path to MyPlugin directory, without slashes.
if ( !defined( 'MyPlugin_DIR' ) ) define( 'MyPlugin_DIR', __DIR__ );

    function CopyFile() {

      $TemplateFileSourceURL = MyPlugin_DIR . '/my-page.php'; // Address to your file in the plugin directory
      $TemplateFileTargetURL = get_stylesheet_directory() . '/page-my-page.php'; // Note the "page-" prefix, it is necessary for WP to select this file instead of the general "page.php". The name after the prefix must match the slug of the page created in WP. 

      if ( !file_exists( $TemplateFileSourceURL ) ) {
        return FALSE;
      }

      $GetTemplate = file_get_contents( $TemplateFileSourceURL );
      if ( !$GetTemplate ) {
        return FALSE;
      }

      $WriteTemplate = file_put_contents( $TemplateFileTargetURL, $GetTemplate );
      if ( !$WriteTemplate ) {
        return FALSE;
      }
      return TRUE;
    }

The other method involves creating the page and saving it in the theme's directory as a template file or just as a page.

To make it a template, you have to add in the first line the following comment:

/*
Template Name: MyPage
*/

MyPage can be any name. Then go to the page editor in WP and select the MyPage template in the right bar. It should be there when you reload the editor's page.

You don't have to make it a template if you add the prefix "page-" to the name of the file in the theme's directory. i.e. page-my-page.php.

The last method is to create the file, save it wherever you want and load WP with a code like this in the first lines:

NOTE: No need to create the page in WP if this method is used, it will do nothing, although you might have to redeclare user's variables and functions.

require_once ('WPBlogUrl/wp-load.php'); // Make sure WPBlogUrl points to the blog's url.

Which method is the best one? Depends on your requirements, but if you don't want to have to add manually the front-end pages in the theme's directory, I think the first option is the best one.

Don't forget to delete the file in the uninstall script when the plugin is deleted.

Hope this helps.

Felipe Alameda A.

于 2012-11-09T08:12:01.630 回答
1

Another simple solution that worked for me is just load the page with

add_action('wp','my_custom_plugin_function');

http://blog.frontendfactory.com/how-to-create-front-end-page-from-your-wordpress-plugin/

于 2016-02-09T21:41:31.903 回答
0

您可以创建新页面模板和新页面并将模板分配给该页面。

然后您可以在页面模板中添加您的自定义功能。这样,您将获得一个新的 url,并且您可以在 WordPress 上下文中运行您的自定义代码。

于 2012-11-09T06:48:14.193 回答