mardi 31 mars 2015

Wordpress custom post type meta box into URL

I am trying to make a custom post type meta box into a field that accepts URL, so the field would contain http://


Here is what I have.


Registering the custom post type:



function custom_post_type() {

// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Adverts', 'Post Type General Name', 'westanglia' ),
'singular_name' => _x( 'Advert', 'Post Type Singular Name', 'westanglia' ),
'menu_name' => __( 'Adverts', 'westa' ),
'parent_item_colon' => __( 'Parent Advert', 'westa' ),
'all_items' => __( 'All Adverts', 'westa' ),
'view_item' => __( 'View Advert', 'westa' ),
'add_new_item' => __( 'Add New Advert', 'westa' ),
'add_new' => __( 'Add New', 'westa' ),
'edit_item' => __( 'Edit Advert', 'westa' ),
'update_item' => __( 'Update Advert', 'westa' ),
'search_items' => __( 'Search Advert', 'westa' ),
'not_found' => __( 'Not Found', 'westa' ),
'not_found_in_trash' => __( 'Not found in Trash', 'westa' ),
);

// Set other options for the Adverts Custom Post Type

$args = array(
'label' => __( 'adverts', 'westa' ),
'description' => __( 'Front page scrolling adverts', 'westa' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'ads' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-slides',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'register_meta_box_cb' => 'add_events_metaboxes'
);

// Registering the Adverts Custom Post Type
register_post_type( 'adverts', $args );

}

// Call the function
add_action( 'init', 'custom_post_type', 0 );


Then I'm adding 3 meta boxes, the last one being the URL one where I'd like to have it with htpp://



add_action( 'add_meta_boxes', 'add_events_metaboxes' );

// Add the Events Meta Boxes
function add_events_metaboxes() {
add_meta_box('wpt_advert_upper', 'Advert Upper Title', 'wpt_advert_upper', 'adverts', 'normal', 'high');
add_meta_box('wpt_advert_lower', 'Advert Lower Title', 'wpt_advert_lower', 'adverts', 'normal', 'high');
add_meta_box('wpt_advert_url', 'Advert Link URL', 'wpt_advert_url', 'adverts', 'normal', 'high');
}

// Add function for the Upper advert title field
function wpt_advert_upper() {
global $post;

// Noncename needed to verify where the data originated
echo '<input type="hidden" name="advertmeta_noncename" id="advertmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the location data if its already been entered
$upperTitle = get_post_meta($post->ID, '_upperTitle', true);

// Echo out the field
echo '<input type="text" name="_upperTitle" value="' . $upperTitle . '" class="adH1" />';

}

// Add function for the Lower advert title field
function wpt_advert_lower() {
global $post;

// Noncename needed to verify where the data originated
echo '<input type="hidden" name="advertmeta_noncename" id="advertmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the location data if its already been entered
$lowerTitle = get_post_meta($post->ID, '_lowerTitle', true);

// Echo out the field
echo '<input type="text" name="_lowerTitle" value="' . $lowerTitle . '" class="adH2" />';

}

function wpt_advert_url() {
global $post;

// Noncename needed to verify where the data originated
echo '<input type="hidden" name="advertmeta_noncename" id="advertmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the location data if its already been entered
$advertUrl = get_post_meta($post->ID, '_advertUrl', true);

// Echo out the field
echo '<input type="text" name="_advertUrl" value="' . $advertUrl . '" class="adUrl" />';

}


And finally saving all the data added to the fields.



function wpt_save_advert_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['advertmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.

$advert_meta['_upperTitle'] = $_POST['_upperTitle'];
$advert_meta['_lowerTitle'] = $_POST['_lowerTitle'];
$advert_meta['_advertUrl'] = $_POST['_advertUrl'];

// Add values of $events_meta as custom fields

foreach ($advert_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}

}

add_action('save_post', 'wpt_save_advert_meta', 1, 2); // save the custom fields


add_action('do_meta_boxes', 'change_image_box');
function change_image_box()
{
remove_meta_box( 'postimagediv', 'adverts', 'side' );
add_meta_box('postimagediv', __('Advert Image'), 'post_thumbnail_meta_box', 'adverts', 'normal', 'high');
}


Sorry it's so long, thought someone might need everything to best advise. Hope someone can help, never done a custom post type before :/


Geoff


Aucun commentaire:

Enregistrer un commentaire