【问题标题】:Frontend custom post submission results in wp_insert_post() undefined前端自定义帖子提交结果 wp_insert_post() undefined
【发布时间】:2016-09-11 10:41:44
【问题描述】:

我已经为这个问题苦苦挣扎了几天,我真的希望你能帮助我。

我创建了一个插件,位于:

'/wp-content/plugins/my-cool-plugin'.

我的插件允许用户通过公共页面上的表单发布自定义帖子类型,基本上任何人都应该能够发布内容。 使用 jQuery,我在提交前端表单时收听,并使用 Ajax 将数据从表单传递到 php 文件以将其处理成帖子。 该文件位于:

'/wp-content/plugins/my-cool-plugin/inc/processor.php'。

以下是我的处理器文件的内容:

$var1= $_POST['some'];
$var2= $_POST['data'];

$new_post = array(
    'post_type'         => 'my_custom_post',
    'post_status'       => 'publish',
    'mcp_1'             => $var1,
    'mcp_2'             => $var2
);

$post_id = wp_insert_post( $new_post, $wp_error );
if ($wp_error == 'false'){
    $post_url = get_permalink( $post_id );
    echo $post_url;
}else {
    // some sort of error
}

当我测试我的表单时,它会导致以下错误:

调用未定义的函数 wp_insert_post() 在线 ... 这是以下行:

$post_id = wp_insert_post( $new_post, $wp_error );

由于我不再在 WordPress 的“范围”内,我是否需要添加一些内容? 还是有另一种(更好的)从前端表单插入自定义帖子的方法?

【问题讨论】:

    标签: ajax wordpress frontend custom-post-type


    【解决方案1】:

    尝试添加

    require(dirname(__FILE__) . '/wp-load.php');
    

    【讨论】:

    • 我的文件不在 WordPress 范围内,因此在需要 wp-load.php 文件的文件夹中查找 wp-load.php 文件。这意味着 require 在我的 /wp-content/plugins/my-cool-plugin/inc/ 文件夹中查找 wp-load.php。
    【解决方案2】:

    为什么你在 wordpress 范围之外运行文件?这不是最好的做法。相反,您可以在 wordpress 范围和用户 wordpress 本机 ajax 中运行它。

    add_action('wp_ajax_yourplugin_create_post', 'yourplugin_create_post');
    add_action('wp_ajax_nopriv_yourplugin_create_post', 'yourplugin_create_post');
    
    function yourplugin_create_post() {
     // your code here
    }
    

    然后你需要将你的 ajax url 从 php 传递到 js:

    function your_plugin_ajaxurl() {
    ?>
    <script type="text/javascript">
    var yourPluginAjaxUrl = "<?php echo admin_url('admin-ajax.php'); ?>";
    </script>
    <?php
    }
    
    add_action('wp_head','your_plugin_ajaxurl');
    

    然后您可以使用您的 ajax 请求,但您需要指明 action:yourplugin_create_post 和 url = yourPluginAjaxUrl

    【讨论】:

    • 您好,尼克,感谢您的回复。这对我找到答案有很大帮助!
    【解决方案3】:

    我花了一些时间来处理尼克的回答,但我终于让它工作了!就像尼克说的那样,我放弃了使用流程文件,因为它超出了 WordPress 的范围。正如尼克建议的那样,我将我的帖子创建从我的过程文件移动到插件初始化文件 (my-cool-plugin.php) 中的一个新函数。这导致了以下新功能:

    add_action('wp_ajax_coolplugin_create_post', 'coolplugin_create_post');
    add_action('wp_ajax_nopriv_coolplugin_create_post', 'coolplugin_create_post');
    
    function coolplugin_create_post() {
        $var1 = $_POST['some'];
        $var2 = $_POST['data'];
    
        $new_post = array(
            'post_type'         => 'my_custom_post',
            'post_status'       => 'publish'
            'post_title'        => 'Some title'
        );
    
        $post_id = wp_insert_post( $new_post, $wp_error );
    
        // check if there is a post id and use it to add custom meta
        if ($post_id) {
            update_post_meta($post_id, 'mcp_1', $var1);
            update_post_meta($post_id, 'mcp_2', $var2);
        }
    
        if ($wp_error == false){
            $post_url = get_permalink( $post_id );
            echo $post_url;
        }else {
            // some sort of error
        }
    }
    

    我还必须更改将自定义值插入到新创建的帖子中的方式,因为wp_insert_post() 函数只接受默认帖子参数(有关这些参数,请参阅wp_insert_post documentation)。

    在我的插入/创建帖子功能旁边,我还必须对我的 javascript 文件进行一些调整,该文件从我的表单中检索填充的数据。因此(正如尼克建议的那样)我需要通过将以下函数添加到 my-cool-plugin.php 来将我的 Ajax URL 从 PHP 传递到 JS,如下所示:

    function your_plugin_ajaxurl() { ?>
        <script type="text/javascript">
            var coolPluginAjaxUrl = "<?php echo admin_url('admin-ajax.php'); ?>";
        </script>
    <?php }
    
    add_action('wp_head','your_plugin_ajaxurl');
    

    通过将 coolPluginAjaxUrl 变量添加到头部,我可以在我的 javascript 中使用 URL 将数据发布到提交表单时,如下所示:

    $( '#form' ).on( 'submit', function(e) {
        var request;
    
        e.preventDefault();
        var val_one = $( '#val-one' ).val();
        var val_two = $( '#val-two' ).val();
    
        var formData = {
            action: 'coolplugin_create_post',
            some: val_one,
            data: val_two,
        };
    
        request = $.ajax({
            type: 'POST',
            url: coolPluginAjaxUrl,
            data: formData,
        });
    });
    

    formData 包含 PHP 中定义的 coolplugin_create_post 操作,请求被发布到头部定义的 coolPluginAjaxUrl URL。

    感谢 Nick 为我指明了正确的方向,我希望我的解决方案也能对其他人有所帮助。请注意,我已经为其他人剥离了一些安全措施,以便其他人轻松了解代码的工作原理。

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 2021-12-25
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      相关资源
      最近更新 更多