Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Created July 2, 2014 16:24
Show Gist options
  • Select an option

  • Save mypacecreator/4f406b08967c6d0ab06b to your computer and use it in GitHub Desktop.

Select an option

Save mypacecreator/4f406b08967c6d0ab06b to your computer and use it in GitHub Desktop.
カテゴリーページにnoindexなど用カスタムフィールド追加(仮)
<?php
//add extra fields to category edit form hook
add_action ( 'edit_category_form_fields', 'extra_category_fields');
//add extra fields to category edit form callback function
function extra_category_fields( $tag ) { //check for existing featured ID
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1">meta robots指定</label></th>
<td>
<input type="text" name="Cat_meta[extra1]" id="Cat_meta[extra1]" size="25" style="width:60%;" value="<?php echo $cat_meta['extra1'] ? $cat_meta['extra1'] : ''; ?>"><br />
<span class="description">(例)noindex, follow</span>
</td>
</tr>
<?php
}
// save extra category extra fields hook
add_action ( 'edited_category', 'save_extra_category_fileds');
// save extra category extra fields callback function
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
//add extra fields to post edit form hook
add_action('admin_menu', 'add_metarobots');
add_action('save_post', 'save_metarobots');
function add_metarobots(){
if(function_exists('add_metarobots')){
add_meta_box('metarobots1', 'meta robots指定', 'insert_metarobots', 'post', 'normal', 'high');
}
}
function insert_metarobots(){
global $post;
wp_nonce_field(wp_create_nonce(__FILE__), 'my_nonce');
echo '<label class="hidden" for="metarobots">meta robots指定</label><input type="text" name="metarobots" size="60" value="'.esc_html(get_post_meta($post->ID, 'metarobots', true)).'" />';
echo '<p>(例)noindex, follow</p>';
}
function save_metarobots($post_id){
$my_nonce = isset($_POST['my_nonce']) ? $_POST['my_nonce'] : null;
if(!wp_verify_nonce($my_nonce, wp_create_nonce(__FILE__))) {
return $post_id;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
if(!current_user_can('edit_post', $post_id)) { return $post_id; }
$data = $_POST['metarobots'];
if(get_post_meta($post_id, 'metarobots') == ""){
add_post_meta($post_id, 'metarobots', $data, true);
}elseif($data != get_post_meta($post_id, 'metarobots', true)){
update_post_meta($post_id, 'metarobots', $data);
}elseif($data == ""){
delete_post_meta($post_id, 'metarobots', get_post_meta($post_id, 'metarobots', true));
}
}
/* 別途、header.phpに以下を追記
<?php
$cat_id = get_query_var('cat');
$cat_data = get_option("category_$cat_id");
if (isset($cat_data['extra1'])){
echo '<meta name="robots" content="'.$cat_data['extra1'].'" />'."\n";
} ?>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment