如何在WordPress前端制作客座博文

np8igboo  于 2022-12-11  发布在  WordPress
关注(0)|答案(1)|浏览(110)

下面是我在WordPress前端页面上创建一个表单的代码,允许访客(访问者)在博客部分发布帖子,而无需登录。
它实际上有3个步骤,我把它应用在WordPress主题222默认主题.
但不幸的是,Form短代码并不适用于我提到的部分,它适用于Body标记之后(无论我在 gutenberg 中的何处回调短代码)。
所以我不知道解决方案:(注:我不想这样做,通过使用任何类型的插件...我知道这听起来很疯狂,但没有插件,请...

**步骤1:**在2022 WordPress主题中添加以下代码到my functions.php的最后一行

//Add Shortcode-Form
require get_template_directory() . '/shortcode-form.php';

**步骤2:**在主题的主文件夹中创建shortcode-form.php文件
**第3步:**添加波纹管行:

add_shortcode( 'themedomain_frontend_post', 'themedomain_frontend_post' );
function themedomain_frontend_post() {
    themedomain_post_if_submitted(); ?>
    <form id="new_post" name="new_post" method="post" enctype="multipart/form-data">

        <p><label for="title"><?php echo esc_html__( 'Title', 'theme-domain' ); ?></label><br />
            <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
        </p>

        <?php wp_editor( '', 'content' ); ?>

        <p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>

        <p><label for="post_tags"><?php echo esc_html__( 'Tags', 'theme-domain' ); ?></label>

            <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" />
        </p>

        <input type="file" name="post_image" id="post_image" aria-required="true">

        <p><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

    </form>
    <?php
}

以及:

function themedomain_post_if_submitted() {
    // Stop running function if the form wasn't submitted
    if ( ! isset( $_POST['title'] ) ) {
        return;
    }

    // Add the content of the form to $post as an array
    $post    = array(
        'post_title'    => $_POST['title'],
        'post_content'  => $_POST['content'],
        'post_category' => array( $_POST['cat'] ),
        'tags_input'    => $_POST['post_tags'],
        'post_status'   => 'draft',   // Could be: publish
        'post_type'     => 'post', // Could be: 'page' or your CPT
    );
    $post_id = wp_insert_post( $post );

    // For Featured Image
    if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
        require_once ABSPATH . 'wp-admin' . '/includes/image.php';
        require_once ABSPATH . 'wp-admin' . '/includes/file.php';
        require_once ABSPATH . 'wp-admin' . '/includes/media.php';
    }
    if ( $_FILES ) {
        foreach ( $_FILES as $file => $array ) {
            if ( $_FILES[ $file ]['error'] !== UPLOAD_ERR_OK ) {
                return 'upload error : ' . $_FILES[ $file ]['error'];
            }
            $attach_id = media_handle_upload( $file, $post_id );
        }
    }
    if ( $attach_id > 0 ) {
        update_post_meta( $post_id, '_thumbnail_id', $attach_id );
    }

    echo 'Saved your post successfully! :)';
}

下面是我在gotenburg主题编辑器中使用的简短代码:
[themedomain_frontend_post]

bmp9r5qi

bmp9r5qi1#

实际上WordPress的短代码应该运行自己的回调函数,所以它除了一个返回变量。在这种情况下,你有回显内部回调函数,这就是为什么所有的输出都在页面的顶部。

add_shortcode( 'themedomain_frontend_post', 'themedomain_frontend_post' );
function themedomain_frontend_post() {
  ob_start();
    themedomain_post_if_submitted();
    $return = "<form id='new_post' name='new_post' method='post'  enctype='multipart/form-data'>
        <p><label for='title'>" . esc_html__('Title','theme-domain') . "</label><br />
            <input type='text' id='title' value=''  tabindex='1' size='20' name='title' />
        </p>". wp_editor( '', 'content' ) ." 
        <p>" . wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ) ."</p>
        <p><label for='post_tags'>" . esc_html__('Tags','theme-domain') . "</label>
        <input type='text' value='' tabindex='5' size='16' name='post_tags' id='post_tags' /></p>
        <input type='file' name='post_image' id='post_image' aria-required='true'>
        <p><input type='submit' value='Publish' tabindex='6' id='submit' name='submit' /></p>
        </form>";
        $return .= ob_get_clean();

        return $return;
}

它将解决问题。

相关问题