我得到了WordPRess插件的以下代码,它在每个页面/后期编辑屏幕上添加了两个自定义输入。然后将这些值保存并输出到前端页面的标题中。
如果代码位于内部,则可以正常工作。php文件并直接放入“wp内容/插件”。然而,如果我把它放在插件(如“wp-content/plugins/myplugin”)中自己的文件夹中,那么在通过编辑屏幕保存帖子/页面时,输入字段不会保存。此外,它不会向前端页面html标题部分输出任何内容。
这似乎是一个被放弃的项目,所以我无法与原始开发人员一起制定解决方案。然而,代码中的某些内容似乎需要更新,以反映代码被放置在插件文件夹中。
这是代码。。。。。
<?php
$clinical_post_types = array();
//add all post types
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
$clinical_post_types[] = $post_type;
//echo \'POST TYPES: \' .$post_type;
}
// Meta box
// -----------------------------------------------------------------
// Add the language metabox on every registered custom post type
function clinical_add_language_metaboxe() {
global $clinical_post_types;
foreach ($clinical_post_types as $post_type) {
add_meta_box(\'clinical_meta_box\', __(\'Search Engine Optimization\', \'clinical\'), \'clinical_meta_custom_box\', $post_type, \'normal\', \'default\');
}
}
// The Post\'s meta fields Metabox
function clinical_meta_custom_box() {
global $post;
echo \'<input type="hidden" name="clinicalmeta_noncename" \'
. \'id="clinicalmeta_noncename" value="\'
. wp_create_nonce( plugin_basename(__FILE__) ) . \'/seo_fields.php" />\';
// Get the meta fields data if its already been entered
$meta_title = get_post_meta($post->ID, \'_clinical_meta_title\', true);
$meta_description = get_post_meta($post->ID, \'_clinical_meta_description\', true);
// Echo out the field
$html = \'<p><label for="clinical_meta_title"><strong>\'. __(\'Title\', \'clinical\') .\'</strong></label></p>\';
$html .= \'<p><input type="text" class="regular-text" name="_clinical_meta_title" id="clinical_meta_title" value="\'. $meta_title .\'" /></p>\';
$html .= \'<p><label for="clinical_meta_description"><strong>\'. __(\'Description\', \'clinical\') .\'</strong></label></p>\';
$html .= \'<p><textarea class="large-text" name="_clinical_meta_description" id="clinical_meta_description">\'. $meta_description .\'</textarea></p>\';
echo $html;
}
// Save the metabox data
function clinical_save_post_meta($post_id, $post) {
global $clinical_post_types;
$key_title = \'_clinical_meta_title\';
$key_description = \'_clinical_meta_description\';
// if we\'re not in a clinical-enabled post type, skip.
if (in_array($post->post_type, $clinical_post_types))
return $post;
// verify this came from our screen and with proper authorization,
// because save_post can be triggered at other times
if ((empty($_POST[$key_title]) && empty($_POST[$key_description])) || empty($_POST[\'clinicalmeta_noncename\']) || !wp_verify_nonce($_POST[\'clinicalmeta_noncename\'], plugin_basename(__FILE__).\'/seo_fields.php\')
|| !current_user_can(\'edit_post\', $post->ID)
) {
return $post->ID;
}
// OK, we\'re authenticated: we need to find and save the data
$title = $_POST[$key_title];
$description = $_POST[$key_description];
// set the post\'s meta title:
$updated_title = update_post_meta($post->ID, $key_title, $title);
$updated_description = update_post_meta($post->ID, $key_description, $description);
// Delete if blank:
if (!$title) delete_post_meta($post->ID, $key_title);
if (!$description) delete_post_meta($post->ID, $key_description);
}
// Filters
function clinical_wp_title_filter($title) {
global $post;
$seo_title = get_post_meta($post->ID, \'_clinical_meta_title\', true);
if (!empty($seo_title))
return $seo_title;
return $title;
}
function clinical_wp_head_action() {
global $post;
$seo_description = get_post_meta($post->ID, \'_clinical_meta_description\', true);
if (!empty($seo_description)) {
echo \'<!-- Clinical CMS SEO -->
<meta name="description" content="\'. esc_attr($seo_description) .\'" />
<!-- END Clinical CMS SEO -->\';
}
}
// Helpers
// Filters and Hooks
add_action(\'admin_init\', \'clinical_add_language_metaboxe\');
add_action(\'save_post\', \'clinical_save_post_meta\', 1, 2);
add_filter(\'wp_title\', \'clinical_wp_title_filter\');
add_action(\'wp_head\', \'clinical_wp_head_action\');
SO网友:Subharanjan
下面是修复和修改后的代码,该代码存在nonce操作问题。
<?php
/*
Plugin Name: Test Plugin
Plugin URI: Test
Description: Test
Author: Test
Version: 007
Author URI:
*/
// add all post types
$clinical_post_types = array();
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
$clinical_post_types[] = $post_type;
}
// Meta box
// Add the language metabox on every registered custom post type
function clinical_add_language_metaboxe() {
global $clinical_post_types;
foreach ( $clinical_post_types as $post_type ) {
add_meta_box( \'clinical_meta_box\', __( \'Search Engine Optimization\', \'clinical\' ), \'clinical_meta_custom_box\', $post_type, \'normal\', \'default\' );
}
}
// The Post\'s meta fields Metabox
function clinical_meta_custom_box() {
global $post;
// Get the meta fields data if its already been entered
$meta_title = get_post_meta( $post->ID, \'_clinical_meta_title\', true );
$meta_description = get_post_meta( $post->ID, \'_clinical_meta_description\', true );
echo \'<input type="hidden" name="clinicalmeta_noncename" id="clinicalmeta_noncename" value="\' . wp_create_nonce( "clinicalmeta_nonce_action" ) . \'" />\';
// Echo out the field
$html = \'<p><label for="clinical_meta_title"><strong>\' . __( \'Title\', \'clinical\' ) . \'</strong></label></p>\';
$html .= \'<p><input type="text" class="regular-text" name="_clinical_meta_title" id="clinical_meta_title" value="\' . $meta_title . \'" /></p>\';
$html .= \'<p><label for="clinical_meta_description"><strong>\' . __( \'Description\', \'clinical\' ) . \'</strong></label></p>\';
$html .= \'<p><textarea class="large-text" name="_clinical_meta_description" id="clinical_meta_description">\' . $meta_description . \'</textarea></p>\';
echo $html;
}
// Save the metabox data
function clinical_save_post_meta( $post_id, $post ) {
global $clinical_post_types;
$key_title = \'_clinical_meta_title\';
$key_description = \'_clinical_meta_description\';
// if we\'re not in a clinical-enabled post type, skip.
if ( in_array( $post->post_type, $clinical_post_types ) ) {
// return $post;
}
// verify this came from our screen and with proper authorization,
// because save_post can be triggered at other times
if ( ( empty( $_POST[ $key_title ] ) && empty( $_POST[ $key_description ] ) ) || empty( $_POST[\'clinicalmeta_noncename\'] ) || ( ! wp_verify_nonce( $_POST[\'clinicalmeta_noncename\'], "clinicalmeta_nonce_action" ) ) || ( ! current_user_can( \'edit_post\', $post->ID ) ) ) {
return $post->ID;
}
// OK, we\'re authenticated: we need to find and save the data
$title = $_POST[ $key_title ];
$description = $_POST[ $key_description ];
// set the post\'s meta title:
$updated_title = update_post_meta( $post->ID, $key_title, $title );
$updated_description = update_post_meta( $post->ID, $key_description, $description );
// Delete if blank:
if ( empty( $title ) ) {
delete_post_meta( $post->ID, $key_title );
}
if ( empty( $description ) ) {
delete_post_meta( $post->ID, $key_description );
}
}
// Filters
function clinical_wp_title_filter( $title ) {
global $post;
$seo_title = get_post_meta( $post->ID, \'_clinical_meta_title\', true );
if ( ! empty( $seo_title ) ) {
return $seo_title;
}
return $title;
}
function clinical_wp_head_action() {
global $post;
$seo_description = get_post_meta( $post->ID, \'_clinical_meta_description\', true );
if ( ! empty( $seo_description ) ) {
echo \'<!-- Clinical CMS SEO --> <meta name="description" content="\' . esc_attr( $seo_description ) . \'" /> <!-- END Clinical CMS SEO -->\';
}
}
// Helpers
// Filters and Hooks
add_action( \'admin_init\', \'clinical_add_language_metaboxe\' );
add_action( \'save_post\', \'clinical_save_post_meta\', 1, 2 );
add_filter( \'wp_title\', \'clinical_wp_title_filter\' );
add_action( \'wp_head\', \'clinical_wp_head_action\' );