如何通过 wordpress 页面上的插件呈现表单?

How to render a form through a plugin on a wordpress page?

我正在练习创建一个wordpress插件,到目前为止我已经通过以下代码安装插件并在管理员用户的管理栏中显示它取得了进展。

if( !defined( 'ABSPATH' ) ) exit;

/**
 * Print SEO tags in the header
 * 
 * @return void
 */

function opt_menu_cactaceas() {
    ?>

    <h1>Registrador de Cactaceas</h1>
    <p>En el siguiente formulario podra registrar diferentes cactaceas</p>
    <p> Formulario 1 </p>

    <?php

}

function agregar_a_menu() {
    add_menu_page('Pluging Cactaceas', 'Plug. Cactaceas', 'manage_options', 'plugin_cactaceas_link', 'opt_menu_cactaceas');

}

add_action("admin_menu", "agregar_a_menu");

我目前的目标是在加载页面时能够做到这一点(例如 mydomain.com/form - 这只是一个页面,不会与任何文章相关联)插件呈现一个表单,随后在我已经配置的 bd Wordpress 的 table 中记录信息。

执行一些测试,我设法找到了 "the_content" 钩子过滤器,通过以下代码,它允许我呈现 HTML 标签,但只在创建的文章中,但正如我希望的那样 HTML 标签直接渲染在页面上,什么也没有出来。

function wetuts_author_bio( $content ) {
    return $content . "<h1>hola diego</h1>";
}

add_filter('the_content', 'wetuts_author_bio');

我的问题是他们是否知道任何钩子或钩子 table 页面呈现的相关钩子会显示给我,因为我找不到任何钩子。

我发现的钩子"the_content"只有在渲染文章时(进入文章时)才会运行,所以它对我没有用。

为此,您必须在自定义插件中为表单创建一个 shortcode,然后您可以直接在页面编辑器中使用该简码。

您可以像这样创建简码:

function registration_form($atts){
    $html = '<h1>Hello</h1>';
    return $html;
}
add_shortcode("registrationform", "registration_form");

然后在任何页面编辑器中添加简码[registrationform]

请查看此 tutorial 了解更多详情。