WordPress: Add page to Yoast SEO breadcrumbs - php

I want to add an extra page to the breadcrumb trail of my site.
The problem is, that I'm using WooCommerce and on the breadcrumb doesn't work right on the my account dashboard. It always shows the following trail:
Home > My account
Even if I'm on a child page like "edit account".
These child pages aren't really pages. They are WooCommerce endpoints on the same page.
It should look like this:
Home > My account > Orders > Order ID
I tried to add a page but couldn't figure it out.
To remove a page from the breadcrumb trail, I'm using the following code:
/**
* Conditionally Override Yoast SEO Breadcrumb Trail
* http://plugins.svn.wordpress.org/wordpress-seo/trunk/frontend/class-breadcrumbs.php
* -----------------------------------------------------------------------------------
*/
add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail' );
function wpse_100012_override_yoast_breadcrumb_trail( $links ) {
global $post;
if ( is_home() || is_singular( 'post' ) || is_archive() ) {
$breadcrumb[] = array(
'url' => get_permalink( get_option( 'page_for_posts' ) ),
'text' => 'Blog',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}
Code is from here: https://wordpress.stackexchange.com/a/121618/96806
I've already changed it to check if I'm on a WooCommerce endpoint.
This code works fine.
But how can I change it to add a page instead of deleting it?
I guess it has something to do with the array_splice ;-)

OK, I have a solution.
Big thanks to the answer from #WebElaine: https://wordpress.stackexchange.com/a/332300/96806
Here's my full code to change the Yoast breadcrumb navigation in the WooCommerce My Account area:
add_filter('wpseo_breadcrumb_links', 'woocommerce_account_breadcrumb_trail');
function woocommerce_account_breadcrumb_trail($links) {
if ( is_wc_endpoint_url() or is_account_page() ) {
$endpoint = WC()->query->get_current_endpoint();
$endpoint_title = WC()->query->get_endpoint_title( $endpoint );
$endpoint_url = wc_get_endpoint_url($endpoint);
if ( is_account_page() && !is_wc_endpoint_url() ) :
//$links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
elseif ( is_wc_endpoint_url( 'edit-account' ) ) :
$links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
elseif ( is_wc_endpoint_url( 'orders' ) ) :
$links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
elseif ( is_wc_endpoint_url( 'view-order' ) ) :
$endpoint_orders = 'orders';
$endpoint_orders_title = WC()->query->get_endpoint_title( $endpoint_orders );
$endpoint_orders_url = wc_get_endpoint_url($endpoint_orders);
$links[2] = array('text' => $endpoint_orders_title, 'url' => $endpoint_orders_url, 'allow_html' => 1);
$links[3] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
elseif ( is_wc_endpoint_url( 'edit-address' ) ) :
$links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
elseif ( is_wc_endpoint_url( 'payment-methods' ) ) :
$links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
elseif ( is_wc_endpoint_url( 'add-payment-method' ) ) :
$endpoint_payment_methods = 'payment-methods';
$endpoint_payment_methods_title = WC()->query->get_endpoint_title( $endpoint_payment_methods );
$endpoint_payment_methods_url = wc_get_endpoint_url($endpoint_payment_methods);
$links[2] = array('text' => $endpoint_payment_methods_title, 'url' => $endpoint_payment_methods_url, 'allow_html' => 1);
$links[3] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);
endif;
}
return $links;
}
I'm happy about every feedback.

Related

Changing a php var form payment handling

Context: I'm creating a WordPress website where users can upload their own posts via frontend by paying a posting fee.
Here is how it works: the user uploads their post data inside a form created with the plugin WP User Frontend Pro; once they click on the "submit" button, they are redirected to the payment page and, once the payment is complete, the system handles the PayPal IPN confirmation and publishes the post.
I have this code snippet created by WP User Frontend Pro that handles the PayPal gateway:
<?php
public function prepare_to_send( $data ) {
$user_id = $data['user_info']['id'];
$listener_url = add_query_arg( 'action', 'wpuf_paypal_success', home_url( '' ) );
//$listener_url = 'http://a53d2f68b609.ngrok.io/?action=wpuf_paypal_success';
//$listener_url = 'https://wpuf.sharedwithexpose.com/?action=wpuf_paypal_success';
$redirect_page_id = wpuf_get_option( 'payment_success', 'wpuf_payment' );
if ( $redirect_page_id ) {
$return_url = add_query_arg( 'action', 'wpuf_paypal_success', untrailingslashit( get_permalink( $redirect_page_id ) ) );
} else {
$return_url = add_query_arg( 'action', 'wpuf_paypal_success', untrailingslashit( get_permalink( wpuf_get_option( 'subscription_page', 'wpuf_payment' ) ) ) );
}
$billing_amount = empty( $data['price'] ) ? 0 : $data['price'];
if ( isset( $_POST['coupon_id'] ) && ! empty( $_POST['coupon_id'] ) ) {
$billing_amount = WPUF_Coupons::init()->discount( $billing_amount, $_POST['coupon_id'], $data['item_number'] );
$coupon_id = $_POST['coupon_id'];
} else {
$coupon_id = '';
}
$data['subtotal'] = $billing_amount;
$billing_amount = apply_filters( 'wpuf_payment_amount', $data['subtotal'] );
$data['tax'] = $billing_amount - $data['subtotal'];
if ( $billing_amount == 0 ) {
wpuf_get_user( $user_id )->subscription()->add_pack( $data['item_number'], $profile_id = null, false, 'free' );
wp_redirect( $return_url );
exit();
}
if ( $data['type'] == 'pack' && $data['custom']['recurring_pay'] == 'yes' ) {
if ( $data['custom']['cycle_period'] == 'day' ) {
$period = 'D';
} elseif ( $data['custom']['cycle_period'] == 'week' ) {
$period = 'W';
} elseif ( $data['custom']['cycle_period'] == 'month' ) {
$period = 'M';
} elseif ( $data['custom']['cycle_period'] == 'year' ) {
$period = 'Y';
}
if ( $data['custom']['trial_duration_type'] == 'day' ) {
$trial_period = 'D';
} elseif ( $data['custom']['trial_duration_type'] == 'week' ) {
$trial_period = 'W';
} elseif ( $data['custom']['trial_duration_type'] == 'month' ) {
$trial_period = 'M';
} elseif ( $data['custom']['trial_duration_type'] == 'year' ) {
$trial_period = 'Y';
}
$paypal_args = [
'cmd' => '_xclick-subscriptions',
'business' => wpuf_get_option( 'paypal_email', 'wpuf_payment' ),
'a3' => $billing_amount,
'mc_amount3' => $billing_amount,
'p3' => ! empty( $data['custom']['billing_cycle_number'] ) ? $data['custom']['billing_cycle_number'] : '0',
't3' => $period,
'item_name' => $data['custom']['post_title'],
'custom' => json_encode(
[
'billing_amount' => $billing_amount,
'type' => $data['type'],
'user_id' => $user_id,
'coupon_id' => $coupon_id,
'subtotal' => $data['subtotal'],
'tax' => $data['tax'],
]
),
'shipping' => 0,
'no_note' => 1,
'currency_code' => $data['currency'],
'item_number' => $data['item_number'],
'rm' => 2,
'return' => $return_url,
'cancel_return' => $return_url,
'notify_url' => $listener_url,
'src' => 1,
'sra' => 1,
'srt' => intval( $data['custom']['billing_limit'] ),
'recurring' => 1,
];
if ( $data['custom']['trial_status'] == 'yes' ) {
$paypal_args['p1'] = $data['custom']['trial_duration'];
$paypal_args['t1'] = $trial_period;
$paypal_args['a1'] = 0;
}
} else {
$paypal_args = [
'cmd' => '_xclick',
'business' => wpuf_get_option( 'paypal_email', 'wpuf_payment' ),
'amount' => $billing_amount,
'item_name' => isset( $data['custom']['post_title'] ) ? $data['custom']['post_title'] : $data['item_name'],
'no_shipping' => '1',
'shipping' => '0',
'no_note' => '1',
'currency_code' => $data['currency'],
'item_number' => $data['item_number'],
'charset' => 'UTF-8',
'rm' => '2',
'custom' => json_encode(
[
'type' => $data['type'],
'user_id' => $user_id,
'coupon_id' => $coupon_id,
'subtotal' => $data['subtotal'],
'tax' => $data['tax'],
]
),
'return' => $return_url,
'notify_url' => $listener_url,
];
}
$this->set_mode();
?>
What I'm focusing on here is the $billing_amount var.
At the moment, this var value is declared via a setting inside the plugin and it is a fixed number value: what I can do is simply change the "posting fee" by changing the value inside the plugin's form settings.
I would like, if possible, to change the value based on some radio/checkbox input fields that the user selects inside the posting form that comes before the payment page (as described before). I already know how to change an input value via JS, but I have no idea on how I could pass this JS value to the $billing_amount var.

Display a list of sub-pages on the current page based on the WP menu

I have created a menu under Appearance > menus called "Sidebar Menu Collection"
This is their heirarchy
Material
- marble
- onyx
- slate
- granite
Applications
- benchtops
- floors
- walls
products
- Slab
- pavers
- cladding
So when I am on the page "Material" I would like to have a list of its child menu (marble,onyx,slate,grantite) only.
If I'm in "Applications" page it would show (benctops,floors,walls). And so on, I would like to add a featured image on every child list based on the page featured image.
Also this will be applicable only to the parent menu (Material,Applications,Products) and if your on the sub-menu no list will be shown
Hello use below code will only show childs of specific pages
wp_list_pages( $args = '' ) {
$defaults = array(
'depth' => 1,
'show_date' => '',
'date_format' => get_option( 'date_format' ),
'child_of' => $post->ID,
'exclude' => '',
'title_li' => __( 'Pages' ),
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'item_spacing' => 'preserve',
'walker' => '',
);
Already had an answer to this, came from this post.
This is working perfectly, the only problem I have now is how to add a featured image on every link.
// add hook
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );
// filter_hook function to react on sub_menu flag
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {
if ( isset( $args->sub_menu ) ) {
$root_id = 0;
// find the current menu item
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
// set the root id based on whether the current menu item has a parent or not
$root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
break;
}
}
// find the top level parent
if ( ! isset( $args->direct_parent ) ) {
$prev_root_id = $root_id;
while ( $prev_root_id != 0 ) {
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->ID == $prev_root_id ) {
$prev_root_id = $menu_item->menu_item_parent;
// don't set the root_id to 0 if we've reached the top of the menu
if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
break;
}
}
}
}
$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
// init menu_item_parents
if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;
if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
// part of sub-tree: keep!
$menu_item_parents[] = $item->ID;
} else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
// not part of sub-tree: away with it!
unset( $sorted_menu_items[$key] );
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
Usage
wp_nav_menu( array(
'menu' => 'Menu Name',
'sub_menu' => true
) );

How to edit this function to improve performance in Wordpress?

I am using a wordpress/woocommerce theme named "invogue". This theme contain some ajax functions that are called using AJAX, but they are super slow >5 sec each.
Is there any way to edit this function to speed things up ?
the following gets the cart items
#GET CART NAV DATA
public function htheme_get_nav_cart_data(){
#GLOBALS
global $wpdb, $hmenu_helper, $woocommerce;
if ( class_exists( 'WooCommerce' ) ) {
#VARAIBLES
$cart_count = $woocommerce->cart->cart_contents_count;
$cart_link = esc_url(get_permalink(get_option('woocommerce_cart_page_id')));
$cart = $woocommerce->cart->get_cart();
$total_quantity = 0;
#ARRAY OF ITEMS
$cart_items = [];
$cart_count = 1;
#FOREACH CART ITEM
foreach($cart as $item){
$image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $item['product_id'] ), 'full' );
$cart_items[] = array(
'id' => $item['product_id'],
'title' => esc_html($item['data']->post->post_title),
'quantity' => $item['quantity'],
'total' => $item['line_subtotal'],
'link' => get_permalink($item['product_id']),
'price' => wc_get_price_decimals(),
'image' => $image[0],
'price_html' => $this->htheme_return_price_html($item['product_id']),
'qty' => esc_html__('Qty', 'invogue'),
);
$total_quantity += $item['quantity'];
$cart_count++;
}
#ECHO JSON
echo json_encode(array(
'status' => 'active',
'count' => $total_quantity,
'url' => $cart_link,
'cart' => $cart_items,
'symbol' => get_woocommerce_currency_symbol(get_option('woocommerce_currency')),
'total' => $woocommerce->cart->get_cart_total(),
));
exit();
} else {
#NOT ACTIVE
echo json_encode(array(
'status' => 'not'
));
exit();
}
}
the following gets the wishlist items
public function htheme_get_nav_wishlist_data(){
#GLOBALS
global $wpdb, $hmenu_helper, $woocommerce;
if ( class_exists( 'WooCommerce' ) ) {
#GET USER ID
$user_ID = get_current_user_id();
#GET USER WISHLIST
$wishlist = esc_attr( get_the_author_meta( 'user_wishlist', $user_ID ) );
#ARGS
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'offset' => 0,
'include' => explode(',', $wishlist)
);
#PRODUCTS
$products = get_posts($args);
#ECHO JSON
echo json_encode($products);
exit();
} else {
#NOT ACTIVE
echo json_encode(array(
'status' => 'not'
));
exit();
}
}

Create Nav menu by Code in wordPress theme

I am basically from .NET and Learning woocommerce themes ..I found this Code segment for Nav menu in Main page
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
Edit :
I found Navigation Menu code in /httpdocs/ECOM/wp-includes / nav-menu.php
function wp_create_nav_menu( $menu_name ) {
return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
}
and this one
function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
$menu_id = (int) $menu_id;
$_menu = wp_get_nav_menu_object( $menu_id );
$args = array(
'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
'slug' => null,
);
// double-check that we're not going to have one menu take the name of another
$_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
if (
$_possible_existing &&
! is_wp_error( $_possible_existing ) &&
isset( $_possible_existing->term_id ) &&
$_possible_existing->term_id != $menu_id
) {
return new WP_Error( 'menu_exists',
/* translators: %s: menu name */
sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
)
);
}
// menu doesn't already exist, so create a new menu
if ( ! $_menu || is_wp_error( $_menu ) ) {
$menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
if ( $menu_exists ) {
return new WP_Error( 'menu_exists',
/* translators: %s: menu name */
sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
)
);
}
$_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
if ( is_wp_error( $_menu ) )
return $_menu;
do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
return (int) $_menu['term_id'];
}
if ( ! $_menu || ! isset( $_menu->term_id ) )
return 0;
$menu_id = (int) $_menu->term_id;
$update_response = wp_update_term( $menu_id, 'nav_menu', $args );
if ( is_wp_error( $update_response ) )
return $update_response;
$menu_id = (int) $update_response['term_id'];
do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
return $menu_id;
}
I searched some post and write some code to update menu
wp_update_nav_menu_object($menu_id, 0, array(
'menu-item-title' => __('Home'),
'menu-item-classes' => 'home',
'menu-item-url' => home_url( '/' ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_object($menu_id, 0, array(
'menu-item-title' => __('Activity'),
'menu-item-classes' => 'activity',
'menu-item-url' => home_url( '/activity/' ),
'menu-item-status' => 'publish'));
wp_update_nav_menu_object($menu_id, 0, array(
'menu-item-title' => __('Members'),
'menu-item-classes' => 'members',
'menu-item-url' => home_url( '/members/' ),
'menu-item-status' => 'publish'));
But this is not working .. What am I missing here ?

WordPress Admin Bar Not Displaying Both Custom Drop-down Menus

I'm having trouble displaying two custom drop-down menus in the WP admin bar. What I want is the first drop-down to display the back-end (editing part) links of every page and the other to display the front-end links of every page. Right now, only the last foreach statement being called displays. Any suggestions are much appreciated.
function admin_bar_link($admin_bar) {
if ( !is_super_admin() || !is_admin_bar_showing() )
return;
$pages = get_pages();
$nurs = get_site_url();
$admin_bar->add_menu(array(
'id' => 'this',
'title' => __($nurs),
'href' => $nurs
));
$admin_bar->add_menu(array(
'id' => 'edit_pages_links',
'title' => __('Edit Pages'),
'href' => false
));
$admin_bar->add_menu(array(
'id' => 'view_pages_links',
'title' => __('View Pages'),
'href' => false
));
foreach ( $pages as $page ) {
$title = $page->post_title;
$url = get_permalink ( $page->ID ) . 'wp-admin/post.php?post=' . $page->ID . '&action=edit'; //edit post url
$admin_bar->add_menu (array(
'title' => $title,
'href' => $url,
'parent' => 'edit_pages_links'
)
);
}//end foreach
foreach ( $pages as $page ) {
$title = $page->post_title;
$url = get_permalink ( $page->ID ) . '?p='. $page->ID;
$admin_bar->add_menu ( array (
'title' => $title,
'href' => $url,
'parent' => 'view_pages_links'
)
);
}//end foreach
}
add_action('admin_bar_menu', 'admin_bar_link');
Firstly your function should accept the $admin_bar variable like this:
function admin_bar_link($admin_bar)
and you don't need global $wp_admin_bar anymore. Then replace these:
$wp_admin_bar->add_menu
with this
$admin_bar->add_menu
Then the menus should appear on the admin bar.

Categories