前端自定义 post 提交结果 wp_insert_post() undefined

Frontend custom post submission results in wp_insert_post() undefined

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

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

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

我的插件允许用户通过 public 页面上的表单 post 自定义 post 类型,基本上任何人都应该能够 post 一些东西。 使用 jQuery,我监听我的前端表单何时提交,并使用 Ajax 我将数据从表单传递到 php 文件以将其处理为 post。 此文件位于:

'/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 'scope' 中了,还需要添加一些东西吗? 或者是否有另一种(更好的)方法从前端表单插入自定义 posts?

尝试添加

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

你为什么运行将文件设置在 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

我花了一些时间来处理 Nick 的回答,但我终于让它起作用了!正如 Nick 所说,我放弃了使用流程文件,因为它超出了 WordPress 的范围。正如 Nick 所建议的,我将我的 post 创建从我的 proces 文件移动到插件初始化文件 (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
    }
}

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

在我的 insert/create post 函数旁边,我还必须对我的 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 到 post 数据,像这样:

$( '#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 操作,请求被 posted 到 coolPluginAjaxUrl URL,定义在头上。

感谢 Nick 为我指明了正确的方向,我希望我的解决方案也能帮助其他人。请注意,为了让其他人更容易理解代码的工作原理,我已经去除了我的代码中的几种安全措施。