wordpress插件中的POST变量

ffscu2ro  于 2023-03-07  发布在  WordPress
关注(0)|答案(1)|浏览(145)

我有下面的代码作为一个插件的模板,希望最终允许轻松地分享内容到社会媒体。
但是,在测试中,似乎没有在POST中设置这些值。
我输入的测试代码不运行-没有错误消息。
我做错了什么?

//Add box to the post edit page with share options
function socialsharePostOptions() {
    global $post;
    $FBtext = get_post_meta( $post->ID, 'FBtext', true );
    $FBshare = get_post_meta( $post->ID, 'FBshare', true );
    
    $TWtext = get_post_meta( $post->ID, 'TWtext', true );
    $TWshare = get_post_meta( $post->ID, 'TWshare', true );
    ?>
    <div>
        <label for="FB">FB post:</label>
        <input type="text" id="FBtext" name="FBtext" value="<?php echo esc_attr( $FBtext ); ?>">
        <input type="checkbox" id="FBshare" name="FBshare" <?php echo $FBshare === 'on' ? 'checked' : ''; ?>>
    </div>
    
    <div>
        <label for="TW">TW post:</label>
        <input type="text" id="TWtext" name="TWtext" value="<?php echo esc_attr( $TWtext ); ?>">
        <input type="checkbox" id="TWshare" name="TWshare" <?php echo $TWshare === 'on' ? 'checked' : ''; ?>>
    </div>
    
    <?php
}
// Add the shares to the post editor
function socialshare_field_add() {
  add_meta_box( 'socialshare_field', 'SocialShare', 'socialsharePostOptions', 'post', 'side', 'default' );
}
add_action('add_meta_boxes', 'socialshare_field_add');

//Run the script on post publish
function socialShareOnPostPublish($post_id){
    //TESTING - this doesn't give any error message (no 'die') !!! ???
    if(isset( $_POST['FBtext'] )){
        wp_die( $_POST['FBtext'] . " and " . $_POST['FBshare']);
    }    
    
 
    // Get the checkbox values
    $FBshare = isset( $_POST['FBshare'] ) ? 'on' : '';
    $TWshare = isset( $_POST['TWshare'] ) ? 'on' : '';
    
    
    
    // If the checkbox is not checked, don't run the script
    if ( $FBshare === 'on' ) {
        $FBtext = isset( $_POST['FBtext'] ) ? $_POST['FBtext'] : '';
        wp_die( $FBtext );
    }
    
    // If the checkbox is not checked, don't run the script
    if ( $TWshare === 'on' ) {
        $TWtext = isset( $_POST['TWtext'] ) ? $_POST['TWtext'] : '';
        wp_die( $TWtext );
    }
}

//add_action( 'publish_post', 'socialShareOnPostPublish' );
add_action( 'save_post', 'socialShareOnPostPublish' ); // TESTING SO ON SAVE

编辑:我尝试使用socialShareOnPostPublish中的update_post_meta将$_ POST数据保存到$post_id

function socialShareOnPostPublish($post_id){
    
    // Get the checkbox values
    $FBshare = isset( $_POST['FBshare'] ) ? 'on' : '';
    $TWshare = isset( $_POST['TWshare'] ) ? 'on' : '';
    
    // Save FBtext and FBshare to post meta
    if ( $FBshare === 'on' ) {
        $FBtext = isset( $_POST['FBtext'] ) ? $_POST['FBtext'] : '';
        update_post_meta( $post_id, 'FBtext', $FBtext );
        update_post_meta( $post_id, 'FBshare', $FBshare );
    }
    
    // Save TWtext and TWshare to post meta
    if ( $TWshare === 'on' ) {
        $TWtext = isset( $_POST['TWtext'] ) ? $_POST['TWtext'] : '';
        update_post_meta( $post_id, 'TWtext', $TWtext );
        update_post_meta( $post_id, 'TWshare', $TWshare );
    }
    
    if(isset( $_POST['FBtext'] )){
        wp_die( $_POST['FBtext'] . " and " . $_POST['FBshare']);
    }  
}

仍然没有错误消息。

wqnecbli

wqnecbli1#

这似乎与复选框有关。一个简单的解决方法是将复选框转换为下拉列表:

//Add box to the post edit page with share options
function socialsharePostOptions() {
    // Check if the post ID is set
    if (isset($_GET['post'])) {
        $post_id = $_GET['post'];
    } else {
        $post_id = 0;
    }
    
    $FBtext = get_post_meta( $post_id, 'FBtext', true );
    $FBshare = get_post_meta( $post_id, 'FBshare', true );

    ?>
    <div>
        <label for="FB">FB post:</label>
        <input type="text" id="FBtext" name="FBtext" value="<?php echo esc_attr( $FBtext ); ?>">
        <select id="FBshare" name="FBshare">
            <option value="yshare" <?php selected($FBshare, 'yshare'); ?>>Share to FB</option>
            <option value="nshare" <?php selected($FBshare, 'nshare'); ?>>Don't share to FB</option>
        </select>  
    </div>
    
    <?php
}
// Add the shares to the post editor
function socialshare_field_add() {
  add_meta_box( 'socialshare_field', 'SocialShare', 'socialsharePostOptions', 'post', 'side', 'default' );
}
add_action('add_meta_boxes', 'socialshare_field_add');

//Run the script on post publish
function socialShareOnPostPublish($post_id){
    
    if (isset($_POST['FBshare'])) {
        update_post_meta($post_id, 'FBshare', sanitize_text_field($_POST['FBshare']));
    }
    if (isset($_POST['FBtext'])) {
        update_post_meta($post_id, 'FBtext', sanitize_text_field($_POST['FBtext']));
    }     
}

//add_action( 'publish_post', 'socialShareOnPostPublish' );
add_action( 'save_post', 'socialShareOnPostPublish' ); // TESTING SO ON SAVE

相关问题