我应该在哪里放置短码文件? (WordPress)

发布于 2025-02-12 13:46:44 字数 646 浏览 1 评论 0原文

我正在制作自定义插件,并试图这次制作短码。因此,我的插件文件夹中有三个文件。

  1. functions.php
  2. data.php
  3. shortcodes.php

,您可以猜到,add_shortCode和do_shortCode函数在shortcodes.php中。我正在关注本文 代码

include('custom-shortcodes.php');

主题函数的 .php。但是,我的短代码文件放在插件的内部,因此我将此代码放入

<?php
    include (ABSPATH . '/wp-content/plugins/my-plugin/shortcodes.php');
?>

function.php中的主题文件夹中以包含我的文件,但请继续错误。我无法保存我的帖子。

我是在做正确的方式还是将其放在错误的文件中?

I'm making a custom plugin and trying to make a shortcode this time. So I have three files in my plugin folder.

  1. functions.php
  2. data.php
  3. shortcodes.php

And as you can guess, add_shortcode and do_shortcode functions are inside of shortcodes.php. I'm following this article and it said to put this code

include('custom-shortcodes.php');

inside of theme's functions.php. However, My shortcode file is placed inside of my plugin so I put this code

<?php
    include (ABSPATH . '/wp-content/plugins/my-plugin/shortcodes.php');
?>

inside of functions.php in themes folder to include my file but keep getting me error. I cannot save my post.

Am I doing the right way or am I putting it in a wrong file with wrong code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-02-19 13:46:44

假设您.../wp-content/plugins/my-plugin目录中有四个.php文件,通常命名为...

  • my-plugin.php-在WordPress加载您的插件。
  • functions.php-您的
  • data.php-您的
  • shortcodes.php-

然后您将您放入my-plugin.php文件代码中:

add_action( 'init', 'my_plugin_startup' );

function my_plugin_startup() {
    require_once( plugin_dir_path( __FILE__ ) . 'functions.php' );
    require_once( plugin_dir_path( __FILE__ ) . 'data.php' );
    require_once( plugin_dir_path( __FILE__ ) . 'shortcodes.php' );
}

这是此require_once(plugin_dir_path(__file_____file_______file_______file_______file______file__ )。加载源文件的代码行。

还有很多其他内容需要介入my-plugin.php文件,您可以阅读有关在这里

开发插件时,不应编辑主题中的任何文件以加载它。 WordPress一旦激活插件就可以做到这一点。您将快捷代码放入您的帖子或页面以使用它。

您向我们展示的文章使用简编函数,但主题编辑是错误的。

Let's say you have four .php files in your .../wp-content/plugins/my-plugin directory, named typically ...

  • my-plugin.php -- the file that gets run when WordPress loads your plugin.
  • functions.php -- yours
  • data.php -- yours
  • shortcodes.php -- yours

Then you put into your my-plugin.php file code like this:

add_action( 'init', 'my_plugin_startup' );

function my_plugin_startup() {
    require_once( plugin_dir_path( __FILE__ ) . 'functions.php' );
    require_once( plugin_dir_path( __FILE__ ) . 'data.php' );
    require_once( plugin_dir_path( __FILE__ ) . 'shortcodes.php' );
}

It's this require_once( plugin_dir_path( __FILE__ ) . 'functions.php' ); line of code that loads your source file.

  • __FILE__ contains the pathname of the presently running php file.
  • plugin_dir_path() retrieves the plugin's directory.
  • you append your filename (. 'functions.php') to that, and
  • use require_once to load it.

There's a bunch of other stuff that needs to go into that my-plugin.php file, which you can read about here.

When you develop a plugin, you should not edit any file in your theme to load it. WordPress does that once you have activated your plugin. You put the shortcode into your post or page to use it.

The article you showed us is accurate on how to create and use a shortcode function, but it's wrong about theme editing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文