I did a metabox that contains a multiple select item that takes his values from a custom post type.
It works but i'm unable to save it...can you help me?
function conduce_palinsesto($post){
?><p>Seleziona il/i conduttore/i</p>
<p>Tieni premuto CTRL per selezionare piĆ¹ conduttori</p>
<?php
global $post;
echo '<select name="conduce[]" id="conduce" multiple="yes">';
$val = get_post_meta($post->ID, 'speaker', true);
$q = get_posts('post_type=speaker');
foreach ($q as $obj)
{
echo '<option value="'.$obj->ID.'" "checked="checked">'.$obj->post_title.'</option>';
}
echo '</select>';
}
This is my save function...that don't work.
add_action('save_post', 'rb_speaker_save_details');
function rb_speaker_save_details($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
} else {
$speak = implode(',', $_POST['conduce']);
update_post_meta($post_id, 'conduce', speak);
}
}
I know it's probably been long solved but here is the answer. I was having the same problem and this is how I solved it.
to save
function rb_speaker_save_details($post_id) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'name_of_nonce' ] ) && wp_verify_nonce( $_POST[ 'name_of_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
if( isset($_POST['conduce']) ) {
$speak = implode(',', $_POST['conduce']);
update_post_meta($post_id, 'conduce', $speak);
}}
and to check the option based on the saved data
echo '<option value="', $slug, '" ';
if(strpos($conduce_val, $slug) !==false) { echo 'selected="selected"'; }
echo '>',the_title(), "</option>\n";
For saving data inserted/selected in a metabox you have to use the save_post action. Maybe you want to have a look at the metabox example in the WP codex.
Related
I use
get_post_type_archive_link
but it's work, only custom post type , but i want get link to the archive post type POST
echo get_permalink( get_option( 'page_for_posts' ) );
With fallback:
function posts_archive_link() {
if ( 'page' == get_option( 'show_on_front' ) ) {
if ( get_option( 'page_for_posts' ) ) {
echo get_permalink( get_option( 'page_for_posts' ) );
} else {
echo home_url( '/?post_type=post' );
}
} else {
echo home_url( '/' );
}
}
I am trying to make custom meta box and display it's value in single post,but code doesn't display value in single post.The value is stored in post at admin side,i want to display that value as front end side as well.Right now i am using twenty seventeen theme.i am using this code for custom meta box in function.php file:
add_action( 'add_meta_boxes', 'm_param_meta_box_add' );
function m_param_meta_box_add() {
add_meta_box( 'm_param_post', 'Box Title', 'm_param_post_meta_box_cb', 'post', 'normal', 'high' );
}
function m_param_post_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
if ( isset( $values['m_meta_description'] ) ) {
$m_meta_description_text = esc_attr( $values['m_meta_description'][0] );
}
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="m_meta_description">Post Description</label></th>
<td><textarea rows="3" cols="50" name="m_meta_description"><?php echo $m_meta_description_text; ?></textarea></td>
</tr>
</table>
<?php
} // close m_param_post_meta_box_cb function
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;
// Make sure your data is set before trying to save it
if( isset( $_POST['m_meta_description'] ) ) {
update_post_meta( $post_id, 'm_meta_description', wp_kses( $_POST['m_meta_description']) );
}
}
add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
if (is_single())
{
global $post;
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo '<meta name="description" content="' . $m_meta_description . '"/>';
}
}
The code that i am trying in single.php is look like:
<?php
add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
if (is_single())
{
global $post;
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo '<meta name="description" content="' . $m_meta_description . '"/>';
}
}
?>
Try to insert below code in your "template-parts/post/content" file
<?php
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo 'meta box value: ' . $m_meta_description;
?>
$meta = get_post_meta($post->ID,'meta-box-text', true);
if($meta != '') {
echo $meta;
}else {
echo "Can't Display The Content";
}
Use this code.
Here meta-box-text is the name attribute.
Don't forget to insert this code in loop
Another way:
$meta_print_value=get_post_meta(get_the_ID(),'meta-box-text',true);
echo($meta_print_value);
I have a custom theme for an online shop (http://themeforest.net/item/mayashop-a-flexible-responsive-ecommerce-theme/2189918) that comes with multiple content types (besides the default post type) and I need to add some kind of vote system to one of this non-default content types ( specifically to the product content type so users can vote the products they like of my shop).
Is there any plugin that provides this funcionality?
Thanks
There are a lot of plugins that do this , you can just google them up,
But if you want to know how it can be easily (and primitivly) done with the help of custom fields , then here you go :
add_action( 'wp_ajax_nopriv_o99__action', 'o99__ajax_cb' );
add_action( 'wp_ajax_o99__action', 'o99__ajax_cb' );
// this fanction is the ajax callback.
function o99__ajax_cb(){
// Verify the nonce to make sure the request is ours and legael...
if( !isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'o99__nonce' ) ) die( '-1' );
$post_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0;
if( ! $post_id ) die( '-1' );
$counter = get_post_meta( $post_id, 'o99_good_post', true );
$counter = absint( $counter );
$cookie = 'o99_post_vote' .$post_id;
if( $counter){
if (!isset($_COOKIE[$cookie]) ) {
$counter++;
}
else {$counter=$counter;}
}
else{
$counter = mt_rand(20,150);
}
update_post_meta( $post_id, 'o99_good_post', $counter ); //hidden field
echo $counter;
die();
}
function o99__good_bad(){
global $post;
// send a simple cookie
$cookie = 'o99_post_vote' .$post_id;
$count = get_post_meta( $post->ID, 'o99_good_post', true ); //hidden field
if (!$count ){
$count = '0';
}
$icon = '<img src="';
$icon .= get_bloginfo('template_url') ; // better using get_stylesheet_uri() now
if (!isset($_COOKIE[$cookie]) ) {
$icon .= '/images/dl-star16.png"/>'; // set up your icons according to case
}
else {
$icon .= '/images/dl-v16.png"/>';
}
?>
<span id="o99__click" title="Click Here to Vote this Post up"><?php echo $icon; ?> click here to vote up
This post was voted <span id="o99__count"><?php echo strip_tags( $count );?>
</span> times</span>
<?php
}
// just injecting the JS into head --
add_action( 'wp_head', 'o99__head' );
function o99__head()
{
if( ! is_singular() ) return;
$post_id = get_queried_object_id();
?>
<script type="text/javascript">
var o99__data = {
action: 'o99__action',
post_id: '<?php echo absint( $post_id ); ?>',
nonce: '<?php echo wp_create_nonce( 'o99__nonce' ); ?>',
}
jQuery(document).ready(function(){
jQuery( '#o99__click' ).click(function(){
jQuery.get(
'<?php echo site_url( 'wp-admin/admin-ajax.php' ); ?>',
o99__data,
function( data ){
if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') != null) {
alert( 'You can only vote once per post!' );
};
if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') == null) {
if( '-1' != data )
{
jQuery( 'span#o99__count' ).html( data );
jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>', 'voted', { expires: 7 });
alert( 'your vote was accepted!' );
};
}
}
);
});
});
</script>
<?php
}
?>
This is a very old function that was hacked a long time ago, it should still work, but maybe need a bit of polish ..
Edit I
some examples of more complex plugins :
http://wordpress.org/extend/plugins/post-ratings/
http://wordpress.org/extend/plugins/post-ratings/screenshots/
http://wordpress.org/extend/plugins/gd-star-rating/
and if you will search the codex you will find much more ..
Those listed above support custom post types .
I have more custom meta in my wordpress and one of them doesn't save anything.
This is the code for saving
add_action('save_post', 'save_details');
function save_details($post_id){
$slug = 'homepage';
/* check whether anything should be done */
$_POST += array("{$slug}_edit_nonce" => '');
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"], plugin_basename( __FILE__ ) ) ){
return;
}
/* Request passes all checks; update the post's metadata */
if (isset($_REQUEST['link_homepage'])) {
update_post_meta($post_id, 'link_homepage', $_REQUEST['link_homepage']);
}
}
Can anyone help me, please?
Thanks!
I have finally created a working code and after i write in the custom field in wordpress and click save removes it from textarea field but saves it and it works.
This is the new coode:
<?php
// add meta box for post types
add_action("admin_init", "admin_init");
function admin_init(){
add_meta_box(
'settings_meta_id',
'Link Homepage',
'settings_meta',
'homepage',
'normal',
'high'
);
}
function settings_meta( $post ){
//global $post;
$values = get_post_custom( $post->ID );
$links = isset( $values['link_homepage'] ) ? $values['link_homepage'][0] : '';
wp_nonce_field( 'homepage_box_nonce', 'link_homepage_nonce' );
?>
<p>
<label for="link_homepage">Link homepage:</label><br />
<textarea cols="100" rows="2" name="link_homepage" style="width:98%;"><?php echo $link_homepage; ?></textarea>
</p>
<?php }
add_action( 'save_post', 'save_custom_details' );
function save_custom_details( $post_id ) {
global $post;
//skip auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
//check for you post type only
if( $post->post_type == "homepage" ) {
if( isset($_POST['link_homepage']) ) { update_post_meta( $post->ID, 'link_homepage', $_POST['link_homepage'] );}
}
}
?>
How to fix that BUG, to keep the text there after saving?
I need advice,
I've installed facebook/google+ on my wordpress category pages.
For example: http://www.sandrophoto.com/category/famous-photographers/
First page has allot of likes, but second and third page only few. This pushed me to think, maybe I should unite those pages into one.
And instead of having separate 'like button' for urls, I would point button only to first page.
http://www.sandrophoto.com/category/famous-photographers/page/2/
http://www.sandrophoto.com/category/famous-photographers/page/3/
Does this make sense?
Any ideas how to implement this? I use this currently:
//get current archives url for fb like and open graph
function get_current_archive_link( $paged = true ) {
$link = false;
if ( is_front_page() ) {
$link = home_url( '/' );
} else if ( is_home() && "page" == get_option('show_on_front') ) {
$link = get_permalink( get_option( 'page_for_posts' ) );
} else if ( is_tax() || is_tag() || is_category() ) {
$term = get_queried_object();
$link = get_term_link( $term, $term->taxonomy );
} else if ( is_post_type_archive() ) {
$link = get_post_type_archive_link( get_post_type() );
} else if ( is_author() ) {
$link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );
} else if ( is_archive() ) {
if ( is_date() ) {
if ( is_day() ) {
$link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
} else if ( is_month() ) {
$link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
} else if ( is_year() ) {
$link = get_year_link( get_query_var('year') );
}
}
}
if ( $paged && $link && get_query_var('paged') > 1 ) {
global $wp_rewrite;
if ( !$wp_rewrite->using_permalinks() ) {
$link = add_query_arg( 'paged', get_query_var('paged'), $link );
} else {
$link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );
}
}
return $link;
}
For both platforms allow you to specify the URL you want Liked/Plused, rather than them trying to determine the URL from the page location and or other meta data. Just specify the same URL in the plugin code to be used on both pages.
Facebook HTML5 version of the plugin would need to be updated like:
<div class="fb-like" data-href="{your URL}"></div>
and Plus 1
<g:plusone annotation="inline" href="{your URL}"></g:plusone>