Ask your WordPress questions! Pay money and get answers fast! (more info)

Backend post editor sidebar module - custom field radio selector WordPress

  • SOLVED

Hello,

I need a function that will make this...

[[LINK href="http://i.imgur.com/1gm8Z.png
"]]http://i.imgur.com/1gm8Z.png[[/LINK]]

in my post editors for these custom post types.

<strong>On Road
Off Road</strong>

These radio buttons basically need to change show/change a custom field.

The custom field is called... <strong>Bike - Info / App Visibility</strong>

The two options that the custom field populates is are... 'yes' and 'no'

The labels for these inputs can be in Titlecase... 'Yes' and 'No'

In my screenshot example, I forgot to included an update button, it will probably need one.

If it can't be done with a radio button, then a drop down with these two options will do fine. The title needs to say 'App Visibility' as per [[LINK href="http://i.imgur.com/1gm8Z.png"]]screenshot[[/LINK]]

All money will go to winning function that I decide to use.

Thanks

Answers (6)

2012-10-10

Christianto answers:

Hi Josh,

Is this metabox under post editor?
I use [[LINK href="http://wordpress.org/extend/plugins/meta-box/"]]rilwis metabox plugin here[[/LINK]], and its awesome!!

you simply activated the plugin and [[LINK href="http://pastebin.com/ipKtSsV0"]]paste this code[[/LINK]] to functions.php
and to access the value you can use ordinary get_meta_post() on the loop:
<?php get_post_meta(get_the_ID(),'_app_visibility', true); ?>
besure to match the the prefix "_app_" I add to the code and the id of each metabox field created for the field name "_app_visibility":
'id' => "{$prefix}visibility"

with this you can assign the metabox to more/less custom post type, by changing this code :
'pages' => array( 'on_road', 'off_road' ),
(as you can see I add the custom field to "on_road" and "off_road" custom post type)

please note that custom post type name cannot contain capital letter or space, you can use capital or space as label.. :D

and you can add different metabox to different custom post type or add other type of custom metabox field like image upload easily..
you can check the demo.php on the plugin demo folder included on the plugin.. :D


Hope this help..


Josh Cranwell comments:


I think I need to solve this problem first [[LINK href="http://wpquestions.com/question/showLoggedIn/id/7425"]]http://wpquestions.com/question/showLoggedIn/id/7425[[/LINK]]

As it seems to be affecting the meta box too :/

2012-10-10

Arnav Joy answers:

try this in functions.php

add_action( 'add_meta_boxes', 'my_meta_box_add' );
function my_meta_box_add()
{
add_meta_box( 'my-meta-box-banner', 'App Visibility', 'my_meta_box_func', 'On Road', 'side', 'high' );
add_meta_box( 'my-meta-box-banner', 'App Visibility', 'my_meta_box_func', 'Off Road', 'side', 'high' );
}


function my_meta_box_func( $post )
{
$values = get_post_custom( $post->ID );
$app_visibilty = isset( $values['app_visibilty'] ) ? $values['app_visibilty'][0] : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<table class="form-table">
<tbody>
<tr><td><input type="radio" name="app_visibilty" <?php checked('Yes',$app_visibilty);?> value="Yes" /> Yes</td></tr>
<tr><td><input type="radio" name="app_visibilty" <?php checked('No',$app_visibilty);?> value="No" /> No</td></tr>
</tbody>
</table>

<?php
}

add_action( 'save_post', 'my_meta_box_save' );

function my_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;

// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);

// Probably a good idea to make sure your data is set
if( isset( $_POST['app_visibilty'] ) )
update_post_meta( $post_id, 'app_visibilty', $_POST['app_visibilty']);


}


Josh Cranwell comments:


I think I need to solve this problem first [[LINK href="http://wpquestions.com/question/showLoggedIn/id/7425"]]http://wpquestions.com/question/showLoggedIn/id/7425[[/LINK]]

As it seems to be affecting the meta box too :/

Thanks

2012-10-10

Martin Pham answers:

please try this
---
functions.php

add_action('admin_menu', 'custom_meta_post_sb');
function custom_meta_post_sb() {
foreach ( (array)get_post_types( array( 'public' => true ) ) as $type ) {
if ( $type == 'On Road' || $type == 'Off Road') {
add_meta_box('custom_meta_post_sb_meta', __('App Visibility', 'marx'), 'custom_meta_post_sb_meta', $type, 'normal', 'high');
}
}
}
function custom_meta_post_sb_meta() {
global $post;
?>
<input type="hidden" name="custom_meta_post_sb_nonce" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" />
<p>
<input type="radio" name="_custom_meta[app_visibility_meta]" id="app_visibility_meta_yes" value="yes" <?php checked( get_post_meta($post->ID, 'app_visibility_meta',true ), 'yes' ); ?> />
<label for="app_visibility_meta_yes"><?php _e( 'Yes', 'marx' ); ?></label>
<br />
<input type="radio" name="_custom_meta[app_visibility_meta]" id="app_visibility_meta_no" value="no" <?php checked( get_post_meta($post->ID, 'app_visibility_meta',true ), 'no' ); ?> />
<label for="app_visibility_meta_no"><?php _e( 'No', 'marx' ); ?></label>
</p>
<?php
}
add_action('save_post', 'custom_meta_post_sb_save', 1, 2);
function custom_meta_post_sb_save($post_id, $post) {
// verify the nonce
if ( !isset($_POST['custom_meta_post_sb_nonce']) || !wp_verify_nonce( $_POST['custom_meta_post_sb_nonce'], plugin_basename(__FILE__) ) )
return $post->ID;
// don't try to save the data under autosave, ajax, or future post.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( defined('DOING_AJAX') && DOING_AJAX ) return;
if ( defined('DOING_CRON') && DOING_CRON ) return;
// is the user allowed to edit the post or page?
if ( ( !current_user_can('edit_page', $post->ID)) || !current_user_can('edit_post', $post->ID ) )
return $post->ID;
// Define all as false, to be trumped by user submission
$custom_meta_defaults = array(
'app_visibility_meta' => '',
);

$meta_parse = wp_parse_args($_POST['_custom_meta'], $custom_meta_defaults);

// store the custom fields
foreach ( (array)$meta_parse as $key => $value ) {

if ( $post->post_type == 'revision' ) return; // don't try to store data during revision save

if ( $value ) {
// save/update
update_post_meta($post->ID, $key, $value);
} else {
// delete if blank
delete_post_meta($post->ID, $key);
}
}
}

use with font-end

global $post;
$_app_vi = get_post_meta($post->ID, 'app_visibility_meta',true );
if($_app_vi == 'yes') {
// do something
} else {
// do else
}

-----------
For modify or add to a new meta box:
- edit/add html code into function custom_meta_post_sb_meta (notice: input/select/texarea ... with <strong>name="_custom_meta[another_custom_meta]"</strong> )
- [defaults] insert new_custom_meta_name into array <strong>$custom_meta_defaults</strong> in function custom_meta_post_sb_save

codex: checked/selected
http://codex.wordpress.org/Function_Reference/checked
http://codex.wordpress.org/Function_Reference/selected

example with selected

function custom_meta_post_sb() {
global $post
$my_option = array(
'Options 1' => 'value1',
'Options 2' => 'value2'
);
?>
<p>
<label for="my_option_meta_input">Example</label>
<select name="_custom_meta[my_option_meta_input]" id="my_option_meta_input">
<?php
foreach ($video_option as $label => $val) {
echo '<option value="'.$val.'" '.selected($val, get_post_meta($post->ID, 'my_option_meta_input',true ), false).'>' .$label. '</option>';
}
?>
</select>
</p>
<?php
}

example add-new meta box

add_action('admin_menu', 'custom_meta_post_sb');
function custom_meta_post_sb() {
foreach ( (array)get_post_types( array( 'public' => true ) ) as $type ) {
if ( $type == 'On Road' || $type == 'Off Road') {
add_meta_box('custom_meta_post_sb_meta', __('App Visibility', 'marx'), 'custom_meta_post_sb_meta', $type, 'normal', 'high');
add_meta_box('custom_meta_post_sb_custom', __('My meta box Name', 'marx'), 'custom_meta_post_sb_custom', $type, 'normal', 'high');
}
}
}
function custom_meta_post_sb_custom() {
// meta fields here
}
function custom_meta_post_sb_meta() {
// meta fields here
}


Josh Cranwell comments:


I think I need to solve this problem first [[LINK href="http://wpquestions.com/question/showLoggedIn/id/7425"]]http://wpquestions.com/question/showLoggedIn/id/7425[[/LINK]]

As it seems to be affecting the meta box too :/

Thanks

2012-10-10

jazbek answers:

Hi Josh,

I would strongly recommend using the Advanced Custom Fields plugin to do what you want. It is free, incredibly easy to use and very powerful. I have used it on many sites. You can add any metaboxes you want to any custom post types.

http://www.advancedcustomfields.com/

Jessica

2012-10-10

Jatin Soni answers:

So you are looking for template code or metabox on edit screen? or both?


Josh Cranwell comments:

I need code to do all of the above. Hopefully the majority is in the form of function that I can drop into the functions.php

But if I need to add other code to get working thats cool.

Thanks.

2012-10-11

Francisco Javier Carazo Gil answers:

You can use a category "Hidden" and later in the filter pre_get_posts exclude them, for example:

function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1,-1347' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );


Where the numbers are the ID of categories excluded.