WordPress社交分享插件POST变量未生效,测试代码无响应排查
WordPress插件POST变量未正确设置问题排查
我开发了一款用于实现内容便捷分享至社交媒体的WordPress插件模板,测试时发现POST变量的值未被正确设置,添加的测试代码既不执行也没有任何错误提示。我尝试在socialShareOnPostPublish函数中使用update_post_meta将POST数据保存到$post_id,但问题仍然存在。
原始代码
//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
编辑后代码
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']); } }
内容的提问来源于stack exchange,提问作者user1505631
相关产品推荐
相关产品推荐

