facebook like button and pagination implementation in wordpress - wordpress

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>

Related

Searching products by tittle name ( Search bar ) backend wordpress woocommerce when is active it messes up the search in Media library

This code works perfect to search products by title on woocommerce .. ( dashbord backend ). However, it's affecting the search on media library, When the code is active it messes up the search in Media library Wordpress.... Is it possible to change the code to target only products instead of images, etc - media library. ???? Thank you.
function __search_by_title_only( $where, &$wp_query )
{
global $wpdb,$typenow,$pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_ids = array();
$terms = explode( ' ', $_GET['s'] );
foreach ( $terms as $term ) {
if ( is_numeric( $term ) ) {
$search_ids[] = $term;
}
if ( $search_term = $_GET['s'] ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $term ) ) . '%\'';
}
}
return $where;
}
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

How to customize WooCommerce Breadcrumbs [duplicate]

I'm using the following function to remove the product title from the breadcrumbs displayed on the product page:
add_filter( 'woocommerce_get_breadcrumb', 'ed_change_breadcrumb' );
function ed_change_breadcrumb( $breadcrumb ) {
if(is_singular()){
array_pop($breadcrumb);
}
return $breadcrumb;
}
It works in that it does remove the title, but it also stops the last category/sub-category from being a hyperlink. How can I fix that?
For example:
Original breadcrumb
<a>Home</a> / <a>Category</a> / <a>Sub Category</a> / Product Title
Result of the above function
<a>Home</a> / <a>Category</a> / Sub Category
I need the Sub Category to still be clickable after removing the product title from the breadcrumbs.
Thanks
Your code works but the last element in the breadcrumbs never contains a link through the code used in global/breadcrumb.php template file on line 34
This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
So you can remove your filter hook and apply the following code in the template file so that it provides a link to the last element when is_product() is true
Note: is_product() - Returns true on a single product page. Wrapper for is_singular()
Replace
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
echo $wrap_after;
}
With
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
if ( is_product() ) {
unset($crumb);
} else {
echo esc_html( $crumb[0] );
}
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
if ( is_product() && sizeof( $breadcrumb ) == $key + 2 ) {
echo '';
} else {
echo $delimiter;
}
}
}
echo $wrap_after;
}

Wordpress - How to save multiple select item value from metabox

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.

how to insert advertisement code into wordpress post

I want to insert advertisement code into WordPress post(detail page) after or before first paragraph. like this post is there any plugin or any idea..
You can try the plugin Ad Inserter.
Or use the example from this WPBeginner tutorial:
<?php
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
Here is the code for showing ad randomly inside WordPress post content-
//Insert ads between first and fourth paragraph of single post content to show it randomly between first and second paragraph.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
// Add code for mobile
$ad_code_mobile = 'AD CODE FOR MOBILE';
// Add code for PC
$ad_code_pc = 'AD CODE FOR PC/DESKTOP/LAPTOP';
if ( is_single() && ! is_admin() ) {
if (!wp_is_mobile()) {
$randnumpc = mt_rand(1,4);
return prefix_insert_after_paragraph( $ad_code_pc, $randnumpc, $content );
}
else {
$randnummobi = mt_rand(1,4);
return prefix_insert_after_paragraph( $ad_code_mobile, $randnummobi, $content );
}
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
That code will show ad randomly within paragraph number 1 and 4. You can place this code at the end of your functions.php of your theme or you can create a separate plugin for this purpose.
Source: https://www.eyeswift.com/random-ad-code-in-wordpress-post-content-without-plugin/

Add login/logout to menu Woocommerce Wordpress

I am trying to figure out how to add a login/logout to the menu. When I add this code to the wordpress header the content and sidebar disappear. How can I add the login/logout to the menu without losing the rest of my page. I have tried adding it in the settings menu and it doesn't work with the theme I'm using.
<ul>
<?php
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id  && !is_user_logged_in()) {
$myaccount_page_url = get_permalink( $myaccount_page_id );
?>
<li><?php _e('Login', 'woocommerce'); ?></li>
<?php
}
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id && is_user_logged_in()) {
$logout_url = wp_logout_url( get_permalink( $myaccount_page_id ) );
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' )
$logout_url = str_replace( 'http:', 'https:', $logout_url );
?>
<li><?php _e('Logout', 'woocommerce'); ?></li>
<?php } ?>
<li><?php _e('Shopping Cart', 'woocommerce'); ?> <?php echo sprintf(_n('(%d)', '(%d)', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></li>
<li><?php _e('Checkout', 'woocommerce'); ?></li>
</ul>
Logout Link:
/?customer-logout=true
Login link sent to My Account page to trigger login:
/my-account/
If you use a plugin like Theme My Login you can just create the link to the login page in your menu. It will display "Login" if the person is not logged in, and "Log Out" if the person is logged in. Hope this helps!
There are many codes that doesn't work. I just found one that works perfectly. Start functions.php with:
add_filter( 'wp_nav_menu_items', 'my_account_loginout_link', 10, 2 );
/**
* Add WooCommerce My Account Login/Logout to Menu
*
* #see https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area
*/
function my_account_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'top') { //change your theme location menu to suit
$items .= '<li><a class="nav-link" href="'. wp_logout_url( get_permalink( wc_get_page_id( 'shop' ) ) ) .'">Sair</a></li>'; //change logout link, here it goes to 'shop', you may want to put it to 'myaccount'
}
elseif (!is_user_logged_in() && $args->theme_location == 'top') {//change your theme location menu to suit
$items .= '<li><a class="nav-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Entrar</a></li>';
}
return $items;
}
Easier way is to change the menu structure
Login (drop down menu)
Lost Password
My Account (drop down menu)
Change Password
Lost password
Logout
then show or hide following page ID in woocommerce-functions.php (:123)
function woocommerce_nav_menu_items( $items, $args ) {
if ( ! is_user_logged_in() ) {
$hide_pages = array();
$hide_pages[] = 20;
$hide_pages = apply_filters( 'woocommerce_logged_out_hidden_page_ids', $hide_pages );
foreach ( $items as $key => $item ) {
if ( ! empty( $item->object_id ) && ! empty( $item->object ) && in_array( $item->object_id, $hide_pages ) && $item->object == 'page' ) {
unset( $items[ $key ] );
}
}
} else {
$hide_pages = array();
$hide_pages[] = 18;
$hide_pages = apply_filters( 'woocommerce_logged_out_hidden_page_ids', $hide_pages );
foreach ( $items as $key => $item ) {
if ( ! empty( $item->object_id ) && ! empty( $item->object ) && in_array( $item->object_id, $hide_pages ) && $item->object == 'page' ) {
unset( $items[ $key ] );
}
}
}
return $items;
}
my login page id was '20', and my account page id was '18'
Hope it helps anyone in need

Resources