I'm new to PHP, and I have to edit an existing PHP code for a WordPress plugin to fit my needs.
What I know about hooks is that we can append an extra function inside a function.
But right now I need to edit an entire PHP file, add an extra line of code in the middle of the page.
The problem I'm facing is that the code is not a set of functions for me to use hooks but instead it's like this:
<?php
global $current_user;
master_user_content_block_start(array(
'title' => esc_html__('My Profile', 'master'),
'title-link-text' => esc_html__('Edit Profile', 'master'),
'title-link' => master_get_template_url('user', array('page_type'=>'edit-profile'))
));
$profile_list = array(
'full_name' => esc_html__('Name', 'master'),
'gender' => esc_html__('Gender', 'master'),
'birth_date' => esc_html__('Birth Date', 'master'),
'country' => esc_html__('Country', 'master'),
'email' => esc_html__('Email', 'master'),
'phone' => esc_html__('Phone', 'master'),
'contact_address' => esc_html__('Contact Address', 'master'),
);
echo '<div class="master-my-profile-wrapper" >';
echo '<div class="master-my-profile-avatar" >';
$avatar = get_the_author_meta('master-user-avatar', $current_user->data->ID);
if( !empty($avatar['thumbnail']) ){
echo '<img src="' . esc_url($avatar['thumbnail']) . '" alt="profile-image" />';
}else if( !empty($avatar['file_url']) ){
echo '<img src="' . esc_url($avatar['file_url']) . '" alt="profile-image" />';
}else{
echo get_avatar($current_user->data->ID, 85);
}
echo '</div>';
$even_column = true;
echo '<div class="master-my-profile-info-wrap clearfix" >';
foreach( $profile_list as $meta_field => $field_title ){
$extra_class = 'master-my-profile-info-' . $meta_field;
$extra_class .= ($even_column)? ' master-even': ' master-odd';
echo '<div class="master-my-profile-info ' . esc_attr($extra_class) . ' clearfix" >';
echo '<span class="master-head" >' . $field_title . '</span>';
echo '<span class="master-tail" >';
if( $meta_field == 'birth_date' ){
$user_meta = master_get_user_meta($current_user->data->ID, $meta_field, '-');
if( $user_meta == '-' ){
echo $user_meta;
}else{
echo master_date_format($user_meta);
}
}else if( $meta_field == 'gender' ){
$user_meta = master_get_user_meta($current_user->data->ID, $meta_field, '-');
if( $user_meta == 'male' ){
echo esc_html__('Male', 'master');
}else if( $user_meta == 'female' ){
echo esc_html__('Female', 'master');
}
}else{
echo master_get_user_meta($current_user->data->ID, $meta_field, '-');
}
echo '</span>';
echo '</div>';
$even_column = !$even_column;
}
echo '</div>';
How can I edit the code of this page without changing the main file content using an additional php file to avoide having problems when the plug-in update.
Example of the edit I want to remove the birth date field and change the word Male to adult and Female to child.
Related
I use the One User Avatar plugin, Inside the settings, there is a check box with the title "Always use the browser file uploader to upload avatars". I tick it. But when I try to change the default avatar, it still uses WordPress media, I came across the following code in the file below :
path : /plugins/one-user-avatar/includes/class-wp-user-avatar-admin.php
public function wpua_add_default_avatar() {
global $avatar_default, $mustache_admin, $mustache_medium, $wpua_avatar_default, $wpua_disable_gravatar, $wpua_functions;
// Remove get_avatar filter
remove_filter( 'get_avatar', array( $wpua_functions, 'wpua_get_avatar_filter' ) );
// Set avatar_list variable
$avatar_list = '';
// Set avatar defaults
$avatar_defaults = array(
'mystery' => __( 'Mystery Man', 'one-user-avatar'),
'blank' => __( 'Blank', 'one-user-avatar'),
'gravatar_default' => __( 'Gravatar Logo', 'one-user-avatar'),
'identicon' => __( 'Identicon (Generated)', 'one-user-avatar'),
'wavatar' => __( 'Wavatar (Generated)', 'one-user-avatar'),
'monsterid' => __( 'MonsterID (Generated)', 'one-user-avatar'),
'retro' => __( 'Retro (Generated)', 'one-user-avatar')
);
$avatar_defaults = apply_filters( 'avatar_defaults', $avatar_defaults );
// No Default Avatar, set to Mystery Man
if ( empty( $avatar_default ) ) {
$avatar_default = 'mystery';
}
// Take avatar_defaults and get examples for unknown#gravatar.com
foreach ( $avatar_defaults as $default_key => $default_name ) {
$avatar = get_avatar( 'unknown#gravatar.com', 32, $default_key );
$selected = ( $avatar_default == $default_key ) ? ' checked="checked" ' : '';
$avatar_list .= sprintf(
'<label><input type="radio" name="avatar_default" id="avatar_%1$s" value="%1$s" %2$s/> ',
esc_attr( $default_key ),
$selected
);
$avatar_list .= preg_replace( "/src='(.+?)'/", "src='\$1&forcedefault=1'", $avatar );
$avatar_list .= ' ' . $default_name . '</label>';
$avatar_list .= '<br />';
}
// Show remove link if custom Default Avatar is set
if ( ! empty( $wpua_avatar_default ) && $wpua_functions->wpua_attachment_is_image( $wpua_avatar_default ) ) {
$avatar_thumb_src = $wpua_functions->wpua_get_attachment_image_src( $wpua_avatar_default, array( 32, 32 ) );
$avatar_thumb = $avatar_thumb_src[0];
$hide_remove = '';
} else {
$avatar_thumb = $mustache_admin;
$hide_remove = ' class="wpua-hide"';
}
// Default Avatar is wp_user_avatar, check the radio button next to it
$selected_avatar = ( 1 == (bool) $wpua_disable_gravatar || 'wp_user_avatar' == $avatar_default ) ? ' checked="checked" ' : '';
// Wrap WPUA in div
$avatar_thumb_img = sprintf( '<div id="wpua-preview"><img src="%s" width="32" /></div>', esc_url( $avatar_thumb ) );
// Add WPUA to list
$wpua_list = sprintf(
'<label><input type="radio" name="avatar_default" id="wp_user_avatar_radio" value="wp_user_avatar" %s /> ',
$selected_avatar
);
$wpua_list .= preg_replace( "/src='(.+?)'/", "src='\$1'", $avatar_thumb_img );
$wpua_list .= ' ' . __( 'One User Avatar', 'one-user-avatar' ) . '</label>';
$wpua_list .= '<p id="wpua-edit"><button type="button" class="button" id="wpua-add" name="wpua-add" data-avatar_default="true" data-title="' . __( 'Choose Image', 'one-user-avatar' ) . ': ' . __( 'Default Avatar', 'one-user-avatar' ) . '">' . __( 'Choose Image', 'one-user-avatar' ) . '</button>';
$wpua_list .= '<span id="wpua-remove-button"' . $hide_remove . '>' . __( 'Remove', 'one-user-avatar' ) . '</span><span id="wpua-undo-button">' . __( 'Undo', 'one-user-avatar' ) . '</span></p>';
$wpua_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="' . $wpua_avatar_default . '">';
$wpua_list .= '<div id="wpua-modal"></div>';
if ( 1 != (bool) $wpua_disable_gravatar ) {
return $wpua_list . '<div id="wp-avatars">' . $avatar_list . '</div>';
} else {
return $wpua_list . '<div id="wp-avatars" style="display:none;">' . $avatar_list . '</div>';
}
}
How can I disable WordPress media and choose and upload a file from my computer?
I found the following code :
$wpua_list .= '<p id="wpua-edit"><button type="button" class="button" id="wpua-add" name="wpua-add" data-avatar_default="true" data-title="' . __( 'Choose Image', 'one-user-avatar' ) . ': ' . __( 'Default Avatar', 'one-user-avatar' ) . '">' . __( 'Choose Image', 'one-user-avatar' ) . '</button>';
I replaced it with the following code :
$wpua_list .= '<p id="wpua-edit"><input name="wpua-file" id="wpua-file" type="file"><button type="submit" class="button" id="wpua-upload" name="wpua-upload" data-avatar_default="true" data-title="' . __( 'Upload', 'one-user-avatar' ) . ': ' . __( 'Default Avatar', 'one-user-avatar' ) . '">' . __( 'Upload', 'one-user-avatar' ) . '</button>';
But when I go to wp-admin/options-discussion.php address and select and upload a photo from the computer, nothing is uploaded.
Where am I doing wrong? Thank you for taking the time to read my question.
I have a odd problem and i'm hoping one of you could guide me to the answer. I am new to PHP and JQuery, so i'm not really sure what is causing this, but essentially, I have a ajax call for a custom search field that works. The "problem" is that after every search result it returns a very random '0' after the results. So for example if I do a search, i'll get something like:
Stack Overflow 0
I've tried seeing what has caused it in the code but I can't iron it out, and as far as I can tell it just...randomly appears there. I don't mind it showing up, if I had a way to apply a css class to it so I could hide it. Anyways, here's my code:
$args = array(
'role' => 'artist',
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
)
);
if (isset($_POST['artistFilter'])){
$name = explode(" ",$_POST['artistFilter']);
$args['meta_query'][] = array(
'key' => 'first_name',
'value' => $name[0],
'compare' => 'LIKE'
);
$args['meta_query'][] = array(
'key' => 'last_name',
'value' => $name[1],
'compare' => 'LIKE'
);
};
if (isset($_POST['mediumType'])){
// WP_User_Query arguments
$args['meta_query'][] = array(
'meta_key' => 'artist_medium',
'value'=> esc_attr($_POST['mediumType']),
'compare' => 'LIKE'
);
};
if (isset($_POST['locationType'])){
$args['meta_query'][] = array(
'meta_key'=>'studio_region_location',
'value' =>$_POST['locationType'],
'compare' => 'LIKE'
);
};
$user_query = new WP_USER_QUERY ($args);
if (! empty($user_query->get_results())){
foreach ($user_query->get_results() as $user){
$artistimage = get_field('primary_image', 'user_' . $user->ID);
echo '<div class="artistSearchColumn">';
if ($artistimage){
echo '<a href="/staging/author/' . esc_html($user->user_login) . '"><img src="' . esc_html($artistimage['url']) . '" alt="Test"/>';
echo '<p>' . $user->first_name. ' ' . $user->last_name .'</p></a>';
}
else {
echo '' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '';
}
echo '</div>';
}
} else {
echo '<h1>No Users Found.</h1>';
}
my jquery:
<script>
jQuery(function($){
var ajaxscript = {ajax_url: './staging/wp-admin/admin-ajax.php'}
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('button').text('Finding Artists...'); // changing the button label
},
success:function(data){
alert(data);
filter.find('button').text('Apply filter'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
});
</script>
Replacing $('#response').html(data) with anything besides data will not return the 0, so I feel like tis related to that.
Thank you for any help!!!
I figured it out, I have to kill the PHP function manually. I simple added
die();
to the end of the script and it doesn't return the 0. Yay! :)
if (! empty($user_query->get_results())){
foreach ($user_query->get_results() as $user){
$artisttour = get_field('artist_tour_status', 'user_'.$user->ID);
if ($user->artist_profile_display == 'Yes'):
if ($artisttour != 'Not on Tour'):
$artistimage = get_field('primary_image', 'user_' . $user->ID);
echo '<div class="artistSearchColumn">';
if ($artistimage){
echo '<a href="/staging/author/' . esc_html($user->user_login) . '">';
echo '<div class="artistBGImage" style="background: url(' . esc_html($artistimage['url']) .');">';
echo '<img src="https://monadnockart.org/wp-content/plugins/foo/arttour25-2020icon-2.jpg" alt="'.esc_html($user->user_login).'s Artist Profile"/>';
echo '</div>';
echo '<p>' . $user->first_name. ' ' . $user->last_name .'</p>';
echo '</a>';
}
else {
echo '<p>' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '</p>';
}
echo '</div>';
else:
$artistimage = get_field('primary_image', 'user_' . $user->ID);
echo '<div class="artistSearchColumn">';
if ($artistimage){
echo '<a href="/staging/author/' . esc_html($user->user_login) . '">';
echo '<div class="artistBGImage" style="background: url(' . esc_html($artistimage['url']) .');">';
echo '</div>';
echo '<p>' . $user->first_name. ' ' . $user->last_name .'</p>';
echo '</a>';
}
else {
echo '<p>' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '</p>';
}
echo '</div>';
endif;
else:
echo '';
endif;
}
}
else {
echo '<h2>No Artists Found.</h2>';
}
die();
}
I'm a designer (not a developer) working on a Wordpress site for a customer. Using the ACF-plugin I've set up a custom field on media files for photo credits. This works fine on featured images, where I can call it in single.php like this:
$post_thumbnail = get_post(get_post_thumbnail_id());
$credit = get_field('media_credit', $post_thumbnail);
if($credit):
echo '<div class="media-credit"><p>Photo: '.$credit.'</p></div>';
endif;
So I know the custom field works, and outputs the right data. However, I can't get it to work on image attachments in posts. What I have is this:
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
//OUTPUT CREDIT
$photographer = get_field( 'media_credit', $attachment_id );
if ($photographer):$media_byline = '<br/><span class="media-credit">Photo: '.$photographer.'</span>';endif;
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
If I remove the if-statement in OUTPUT it shows «Photo: » within the captions, after the text like it should, but it doesn't get any data. What am I missing?
(BTW – I know there are plugins that outputs image credits, but they tend to have styles and features I have to override, resulting in a spaghetti mess I'd hate to hand over to the next guy working on this site.)
Finally got this to work! :-D Instead of using $attachment_id, I got the ID from $attr, and then stripped the 'attachment_' prefix from output.
I also made separate fields for photographer and bureau, but I guess that's beside the point.
Here is the code:
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
$credit_id = $attr['id'];
$credit_id = str_replace( 'attachment_', '', $credit_id );
$photographer = get_field( 'media_credit', $credit_id );
$bureau_credit = get_field( 'media_bureau', $credit_id );
if ( $photographer && $bureau_credit ): $dash = ' / ';
endif;
if ( $photographer || $bureau_credit ): $media_byline = '<br/><span class="media-credit">PHOTO: '
. $photographer . ''
. $dash . '<span class="bureau-credit">'
. $bureau_credit
. '</span></span>';
endif;
return '<div id="attachment_' . $credit_id . '"'
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
This solution is something I lifted from the AFC Media Credit-plugin, so credits to the developer.
I hope this is useful for anybody who wants to achieve something similar.
Sorry about the code format, CTRL+K is said to move things 4 spaces. But it doesnt work in Chrome:(
Update: Amazing, I tried this 4 times. EAch time it triggered a Chrome Shortcut. I gave up and submitted this. It gave an error saying that I had code and did not properly format it. Then amazingly, Ctrl+K works. Not sure why this is so hard...
ANYWAYS,
I upgraded to WP 5.0 on a customer site and now all of the custom fields do not save.... I was using custom code and now the fields do not show in wordpress. It seems that everything I try today is not working. StackOverflow is now the latest to join this...
Id appreciate some help with this if anyone can read the last result.
-------------------------
$meta_box = array(
'id' => 'tunebot-meta-marketing-box',
'title' => 'Enable Marketing Links?',
'page' => 'page',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Hide Title?',
'id' => 'tunebot_hide_titler',
'type' => 'checkbox'
),
array(
'name' => 'Display Image?',
'id' => 'tunebot_display_image',
'type' => 'checkbox'
),
array(
'name' => 'Enable Top Banner?',
'id' => 'tunebot_banner',
'type' => 'checkbox'
),
array(
'name' => 'Enable Marketing Links?',
'id' => 'tunebot_marketing_links',
'type' => 'checkbox'
),
)
);
add_action('admin_init', 'tunebot_add_box');
// Add meta box
function tunebot_add_box() {
global $meta_box;
add_action('post_submitbox_misc_actions', 'tunebot_show_box');
//add_meta_box($meta_box['id'], $meta_box['title'], 'tunebot_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
/**
* Shows a box with options bellow it, under the text of the body.
*/
function tunebot_show_box() {
global $meta_box, $post;
// Use nonce for verification
$output = '<input type="hidden" name="tunebot_meta_box_nonce" value="' . wp_create_nonce(basename(__FILE__)) . '"/>';
foreach($meta_box['fields'] as $field) {
// get current post meta data
$postId = $post->ID;
$key = 'tb_' . $field['id'];
$meta = get_post_meta($postId, $field['id'], TRUE);
$output .= '<div class="misc-pub-section misc-pub-section-last">';
switch($field['type']) {
case 'text':
$output .= '<input type="text" name="' . $key . '" id="' . $key . '"
value="' . $meta ? $meta : $field['std'] . '" size="30" style="width:97%"/>' . '<br/>' .
$field['desc'];
break;
case 'textarea':
$output .= '<textarea name="' . $key . '" id="' . $key . '" cols="60" rows="4"
style="width:97%">' . $meta ? $meta : $field['std'] . '</textarea>' . '<br/>' .
$field['desc'];
break;
case 'select':
$output .= '<select name="' . $key . '" id="' . $key . '">';
foreach($field['options'] as $option) {
$output .= '
<option
' . $meta == $option ? ' selected="selected"' : '' . '>' . $option . '</option>';
}
$output .= '</select>';
break;
case 'radio':
foreach($field['options'] as $option) {
$output .= '<input type="radio" name="' . $key . '" value="' . $option['value'] . '"' . (($meta == $option['value']) ? ' checked="checked"' : '' . ' />' . $option['name']);
}
break;
case 'checkbox':
$output .= '<input type="checkbox" name="' . $key . '" id="' . $key . '"' . ($meta == "on" ? ' checked="checked"' : '') . ' />';
break;
}
$output .= '<label for="' . $key . '">' . $field['name'] . '</label></div>';
}
echo $output;
}
add_action('save_post', 'tunebot_save_data');
function tunebot_save_data($post_id) {
global $meta_box;
// verify nonce
if(isset($_POST['tunebot_meta_box_nonce']) && !wp_verify_nonce($_POST['tunebot_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if(isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
if(!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif(!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach($meta_box['fields'] as $field) {
if(isset($field['id'])) {
$key = 'tb_' . $field['id'];
if(isset($_POST[$key])) {
$new = $_POST[$key];
} else {
$new = FALSE;
}
$old = get_post_meta($post_id, $field['id'], TRUE);
if($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
return $post_id;
}
I am trying to write a content rating script where the user can select what type of rating to give to their article (for instance, what age group the articles suits for).
I am using a Wordpress star rating script as a template.
This part of the script is where the user selects the rating:
function pn_apr_meta_box_form( $post )
{
wp_nonce_field( 'pn_apr_meta_box_nonce', 'pn_apr_meta_box_nonce_field' );
$current_post_rating = get_post_meta( $post->ID, PN_APR_RATING_META_KEY, true );
echo '<label for="pn_apr_rating">' . __( 'Choose a rating for this post:', 'author-post-ratings' ) . '</label> ';
echo '<select name="pn_apr_rating" id="pn_apr_rating">';
echo '<option value="unrated"' . selected( $current_post_rating, 0, false ) . '>' . __( 'Unrated', 'author-post-ratings' ) . '</option>';
for ( $i = 1; $i <= 5; $i++ ) {
echo '<option value="' . $i . '"' . selected( $current_post_rating, $i, false ) . '>' . sprintf( _n( '%1s Star', '%1s Stars', $i, 'author-post-ratings' ), $i ) . '</option>';
}
echo '</select>';
}
This part of the script outputs the ratings:
if ( $rating ) {
$output = null;
$output .= '<div class="author-post-rating">';
$output .= '<span class="author-post-rating-label">' . esc_attr( $pn_apr_settings['label_text'] ) . '</span> ';
$output .= '<span class="author-post-rating-stars" title="' . sprintf( __( '%1$d out of %2$d stars', 'author-post-ratings' ), $rating, 5 ) . '">';
// Output active stars
for ( $i = 1; $i <= $rating; $i++ ) {
$output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-active.png" />';
}
// Output inactive stars
for ( $i = $rating + 1; $i <= 5; $i++ ) {
$output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-inactive.png" />';
}
$output .= '</span>' . "\n";
$output .= '</div><!-- .author-post-rating -->';
if ( true == $return ) { return $output; }
// We don't need to use "else" here, since calling return will automatically stop further execution of this function.
echo $output;
}
Now, I want to change this script so that it becomes a content rating script (rather than star rating one). I want to offer these choices for the user:
G — Suitable for all audiences
PG — Possibly offensive, usually for audiences 13 and above
R — Intended for adult audiences above 17
X — Even more mature than above
Question: How can I change the script so that if the user selects for instance PG, then it will output the text Suitable for age 13 and up.
EDIT:
To Shawn, observe the following code:
function rating_select_cb( $post ) {
global $wpdb;
$value = get_post_meta($post->ID, 'rating', true);
echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Select a rating:<br></label>';
// build an array of each available rating
$ratings = array(
1 => 'G — Suitable for all audiences',
2 => 'PG — Possibly offensive, usually for audiences 13 and above',
3 => 'R — Intended for adult audiences above 17',
4 => 'X — Even more mature than above'
);
echo '<select name="rating">';
echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> None... </option>';
// output each rating as an option
foreach ($ratings as $id => $text) {
echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
}
echo '</select>';
echo '</span></div>';
}
Here's something that should get you to where you need to be:
Add to your functions.php:
add_action( 'add_meta_boxes', 'rating_select_box' );
function rating_select_box() {
add_meta_box(
'rating_select_box', // id, used as the html id att
__( 'Select rating:' ), // meta box title, like "Page Attributes"
'rating_select_cb', // callback function, spits out the content
'post', // post type or page. We'll add this to posts only
'side', // context (where on the screen
'low' // priority, where should this go in the context?
);
}
function rating_select_cb( $post )
{
global $wpdb;
$value = get_post_meta($post->ID, 'rating', true);
echo '<div class="misc-pub-section misc-pub-section-last">
<span id="timestamp">'
. '<label>Select a rating:<br></label>';
$selected = ($value == $result->post_name) ? ' selected="selected" ' : null;
echo '<select name="rating">';
echo '<option value="" default="default"> None... </option>';
echo '<option value="0" '.$selected.'> G — Suitable for all audiences </option>';
echo '<option value="1" '.$selected.'> PG — Possibly offensive, usually for audiences 13 and above </option>';
echo '<option value="2" '.$selected.'> R — Intended for adult audiences above 17 </option>';
echo '<option value="3" '.$selected.'> X — Even more mature than above </option>';
echo '</select>';
echo '</span></div>';
}
add_action( 'save_post', 'save_metadata');
function save_metadata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if( empty($postid) ) return false;
if ( is_null($_REQUEST["rating"]) ) {
delete_post_meta($postid, 'rating');
} else {
update_post_meta($postid, 'rating', $_REQUEST['rating']);
}
}
The above code does this:
1. Adds a new metabox to your posts
2. Adds a select box with the rating values
3. Saves the metadata with the post
To access your metadata in your templates:
$meta = get_post_custom($post->ID);
echo $meta['rating'][0];
To have your template display a custom string use something like:
switch ( $meta['rating'][0] ) {
case 0:
echo "This is rated PG";
break;
case 1:
echo "This is rated G";
break;
case 2:
echo "This is rated R";
break;
case 3:
echo "Ug oh! This is rated X!";
break;
default:
echo "This is not yet rated.";
}
**Edit: This code provides full functionality.. you could abandon your current solution if it works for you