I was wondering if its possible to add a little box, like the 'excerpt' box to all posts in wordpress so that i can enter a URL.
Cheers,
You can use the add_meta_box function. You also need a callback function that outputs the form html on the post screen and a save function.
Here is a basic example that adds a URL meta box to lower right hand side of the post screen.
add_action( 'add_meta_boxes', 'c3m_sponsor_meta' );
function c3m_sponsor_meta() {
add_meta_box( 'c3m_meta', 'Sponsor URL Metabox', 'c3m_sponsor_url_meta', 'post', 'side', 'high' );
}
function c3m_sponsor_url_meta( $post ) {
$c3m_sponsor_url = get_post_meta( $post->ID, '_c3m_sponsor_url', true);
echo 'Please enter the sponsors website link below';
?>
<input type="text" name="c3m_sponsor_url" value="<?php echo esc_attr( $c3m_sponsor_url ); ?>" />
<?php
}
add_action( 'save_post', 'c3m_save_project_meta' );
function c3m_save_project_meta( $post_ID ) {
global $post;
if( $post->post_type == "post" ) {
if (isset( $_POST ) ) {
update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
}
}
}
Edit: Corrected namespace error in code above.
Related
I've added a custom field to variable products in the WC admin. Using the action:
action_woocommerce_variation_options_pricing
But it's displaying in the wrong location. I'd ideally like the checkbox to display in the options section above. But i can't find the action to move it there.
I've spent ages looking through the WC docs, but can't find the correct action. Does anyone know the correct way for me to move the checkbox to this location?
You can use the woocommerce_variation_options action hook, to add a custom checkbox to the WooCommerce product variation options.
(Copied from: views/html-variation-admin.php, line 188)
<?php do_action( 'woocommerce_variation_options', $loop, $variation_data, $variation ); ?>
So you get:
// Add checkbox
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
$is_checked = get_post_meta( $variation->ID, '_mycheckbox', true );
if ( $is_checked == 'yes' ) {
$is_checked = 'checked';
} else {
$is_checked = '';
}
?>
<label class="tips" data-tip="<?php esc_attr_e( 'This is my data tip', 'woocommerce' ); ?>">
<?php esc_html_e( 'Checkbox:', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_checkbox" name="_mycheckbox[<?php echo esc_attr( $loop ); ?>]"<?php echo $is_checked; ?>/>
</label>
<?php
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3);
// Save checkbox
function action_woocommerce_save_product_variation( $variation_id, $i ) {
if ( ! empty( $_POST['_mycheckbox'] ) && ! empty( $_POST['_mycheckbox'][$i] ) ) {
update_post_meta( $variation_id, '_mycheckbox', 'yes' );
} else {
update_post_meta( $variation_id, '_mycheckbox', 'no' );
}
}
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );
I have a custom meta checkbox in a custom post type that until this afternoon was working fine, I went into add some new post types and the checkbox won't save I did add the isset value in the show_meta but it was still working after I did this and does not start working again if I remove it, and nothing is showing up in my debug can anybody help?:
function add_tiles_meta_box() {
add_meta_box(
'tiles_meta_box', // $id
'Show in Large Tiles?', // $title
'show_tiles_meta_box', // $callback
'tiles', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_tiles_meta_box' );
function show_tiles_meta_box( $post, $metabox ) {
$tiles = get_post_meta( $post->ID, 'tiles', true );
wp_nonce_field( basename(__FILE__), 'tiles_meta_box_nonce' );
?>
<!-- All fields will go here -->
<p>
<input type="checkbox" name="tiles" <?php echo (isset($_POST['tiles'])?"value='on'":"value='off'")?> <?php checked( $tiles, 'on' ); ?>/> Yes this is a large tile.
</p>
<?php
}
$post_ID = $post->ID;
function save_tiles_meta($post_ID) {
$post_ID = (int) $post_ID;
$post_type = get_post_type($post_ID);
$post_status = get_post_status( $post_ID );
if ($post_type) {
update_post_meta($post_ID, "tiles", $_POST["tiles"]);
}
return $post_ID;
}
add_action( 'save_post', 'save_tiles_meta' );
Your save_tiles_meta function is unnecessarily convoluted. Something may be failing along the way. Here's a more standard way to do that:
add_action( 'save_post', 'save_tiles_meta' );
function save_tiles_meta(){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}
Try with below code
add_action( 'save_post', 'save_tiles_meta',99,1 );
function save_tiles_meta($postId){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}
I can add extra form elements to category editor using the following hook:
add_action ( 'edit_category_form_fields', 'my_custom_function');
What I wonder is, is there a way to add extra form elements to a "custom post type category"
I checked the hook reference, I found hooks for terms (e.g. edit_term_taxonomy) but not categories.
Thanks
This will add a field called 'TERM META TEXT' to your categories. I did take out the nonce but I really think it should go back in. Also, it's just better to have some sanitization vs. none. This example includes javascript and CSS hooks which you may or may not need but you can quickly see how all the parts go together.
// REGISTER TERM META
add_action( 'init', '___register_term_meta_text' );
function ___register_term_meta_text() {
register_meta( 'term', '__term_meta_text', '___sanitize_term_meta_text' );
}
// SANITIZE DATA
function ___sanitize_term_meta_text ( $value ) {
return sanitize_text_field ($value);
}
// GETTER (will be sanitized)
function ___get_term_meta_text( $term_id ) {
$value = get_term_meta( $term_id, '__term_meta_text', true );
$value = ___sanitize_term_meta_text( $value );
return $value;
}
// ADD FIELD TO CATEGORY TERM PAGE
add_action( 'category_add_form_fields', '___add_form_field_term_meta_text' );
function ___add_form_field_term_meta_text() { ?>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<div class="form-field term-meta-text-wrap">
<label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label>
<input type="text" name="term_meta_text" id="term-meta-text" value="" class="term-meta-text-field" />
</div>
<?php }
// ADD FIELD TO CATEGORY EDIT PAGE
add_action( 'category_edit_form_fields', '___edit_form_field_term_meta_text' );
function ___edit_form_field_term_meta_text( $term ) {
$value = ___get_term_meta_text( $term->term_id );
if ( ! $value )
$value = ""; ?>
<tr class="form-field term-meta-text-wrap">
<th scope="row"><label for="term-meta-text"><?php _e( 'TERM META TEXT', 'text_domain' ); ?></label></th>
<td>
<?php wp_nonce_field( basename( __FILE__ ), 'term_meta_text_nonce' ); ?>
<input type="text" name="term_meta_text" id="term-meta-text" value="<?php echo esc_attr( $value ); ?>" class="term-meta-text-field" />
</td>
</tr>
<?php }
// SAVE TERM META (on term edit & create)
add_action( 'edit_category', '___save_term_meta_text' );
add_action( 'create_category', '___save_term_meta_text' );
function ___save_term_meta_text( $term_id ) {
// verify the nonce --- remove if you don't care
if ( ! isset( $_POST['term_meta_text_nonce'] ) || ! wp_verify_nonce( $_POST['term_meta_text_nonce'], basename( __FILE__ ) ) )
return;
$old_value = ___get_term_meta_text( $term_id );
$new_value = isset( $_POST['term_meta_text'] ) ? ___sanitize_term_meta_text ( $_POST['term_meta_text'] ) : '';
if ( $old_value && '' === $new_value )
delete_term_meta( $term_id, '__term_meta_text' );
else if ( $old_value !== $new_value )
update_term_meta( $term_id, '__term_meta_text', $new_value );
}
// MODIFY COLUMNS (add our meta to the list)
add_filter( 'manage_edit-category_columns', '___edit_term_columns' );
function ___edit_term_columns( $columns ) {
$columns['__term_meta_text'] = __( 'TERM META TEXT', 'text_domain' );
return $columns;
}
// RENDER COLUMNS (render the meta data on a column)
add_filter( 'manage_category_custom_column', '___manage_term_custom_column', 10, 3 );
function ___manage_term_custom_column( $out, $column, $term_id ) {
if ( '__term_meta_text' === $column ) {
$value = ___get_term_meta_text( $term_id );
if ( ! $value )
$value = '';
$out = sprintf( '<span class="term-meta-text-block" style="" >%s</div>', esc_attr( $value ) );
}
return $out;
}
// ADD JAVASCRIPT & STYLES TO COLUMNS
add_action( 'admin_enqueue_scripts', '___admin_enqueue_scripts' );
function ___admin_enqueue_scripts( $hook_suffix ) {
if ( 'edit-tags.php' !== $hook_suffix || 'category' !== get_current_screen()->taxonomy )
return;
// ADD YOUR SUPPORTING CSS / JS FILES HERE
// wp_enqueue_style( 'wp-color-picker' );
// wp_enqueue_script( 'wp-color-picker' );
add_action( 'admin_head', '___meta_term_text_print_styles' );
add_action( 'admin_footer', '___meta_term_text_print_scripts' );
}
// PRINT OUR CUSTOM STYLES
function ___meta_term_text_print_styles() { ?>
<style type="text/css">
.column-__term_meta_text { background-color:rgb(249, 249, 249); border: 1px solid lightgray;}
.column-__term_meta_text .term-meta-text-block { display: inline-block; color:darkturquoise; }
</style>
<?php }
// PRINT OUR CUSTOM SCRIPTS
function ___meta_term_text_print_scripts() { ?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
$input_field = $( '.term-meta-text-field' );
// console.log($input_field); // your input field
} );
</script>
<?php }
Yes, you can, it is called taxonomy (the custom category). The hook you are looking for is:
$taxonomy_edit_form_fields
The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug (custom taxonomy you have registered. Like e.g. for care_types it will be car_types_edit_form_fields).
Source:
https://developer.wordpress.org/reference/hooks/taxonomy_edit_form_fields/
Note the hook you referred above is deprecated see this page:
This hook has been deprecated. Use {$taxonomy}_edit_form_fields
instead.
Source:
https://developer.wordpress.org/reference/hooks/edit_category_form_fields/
I'm sooooo close - but I can't get this last little bit.
I am adding the authors name to books we have for sale in woo. The style needs to be different from the book title so I'm using a custom meta field to put it under the title and style it the way I want. I have everything working and where I want it, now I just need for it only show on a particular category instead of on all products.
There seems to me to be two ways to do this.
1 - Only display it for products in a particular category
2 - Only display it if there is content.
I'm currently trying to only display if it's a particular category, but while writing this, its seems a more elegant solution to only display if content exists.
Here's what I have
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
This doesn't display anywhere, but when I take out the if statement it shows on all products.
Any idea where I went awry?
Here is the answer in context Helga - this snippet will add a custom meta field to the woocommerce product editing dashboard, save the content and display it under the product title on the single product view.
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
What if you simply test for the presence of the meta? If you don't save the meta in the other categories it will never display:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Helga - Thanks for your interest and help. I had a little time so I decided to try the other route of only displaying the meta if there was content instead of based on category. This may be a more flexible solution anyway, leaving the field open to any sort of subhead they want instead of limiting it only to Authors.
This the code that worked:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
I have created a 2x shortcodes that displays a youtube video and vimeo video when the user enters the videos id in it, for emample: [youtube id=""] [vimeo id=""].
Instead of entering this in my post I am trying to create a custom meta box that the user can enter the id in and then this is displayed.
So far I have been able to create a plugin to display the meta box but I'm not sure how to get it to save the youtube/vimeo ID and display the video.
Will I need 2x different meta boxes? One that the user can enter the youtube video ID in and the other where the vimeo ID can be entered and then this is displayed on the front end?
The code I have for the meta box is:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'videos', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Youtube/Vimeo ID:</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_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['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
?>
I also think I need to run a loop on my single-video.php template so that wordpress knows to display the ID from the custom meta box. For the loop this is all I have so far:
$args = array( 'post_type' => 'videos', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
That isnt finished though so I would really appreciate any help.
You are going in correct way. You need to create metabox for selecting video_type it will youtube/vimeo. then you need to give one text box for adding id for respective video type.
code will look like as follows
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'videos', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$vedio_type = get_post_meta($post->ID,'my_vedio_type',true);
$vedio_id = get_post_meta($post->ID,'my_meta_box_text',true);
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Select vedio type:</label>
<!-- added select for selecting vedio type -->
<select name="my_vedio_type" id="my_vedio_type">
<option <?php echo ($vedio_type == 'youtube') ? "selected='selected'" : "" ;?> value="youtube">Youtube</option>
<option <?php echo ($vedio_type == 'vimeo') ? "selected='selected'" : "" ;?> value="vimeo">Vimeo</option>
</select>
<!-- added select for selecting vedio type -->
</p>
<p>
<label for="my_meta_box_text">Youtube/Vimeo ID:</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $vedio_id; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_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['my_vedio_type'] ) )
update_post_meta( $post_id, 'my_vedio_type', wp_kses( $_POST['my_vedio_type'], $allowed ) );
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
?>
after that you can retrieve the these values using following code in custom post type single.php file
<?php
if(get_post_meta($post->ID,'my_vedio_type',true) == "youtube"){
$youtube_id = get_post_meta($post->ID,'my_meta_box_text',true);
}
if(get_post_meta($post->ID,'my_vedio_type',true) == "vimeo"){
$vimeo_id = get_post_meta($post->ID,'my_meta_box_text',true);
}
?>