WordPress Admin Bar Not Displaying Both Custom Drop-down Menus - php

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.

Related

How to display post name and post link in elementor SELECT2 control

i have created a elementor control in this I have displayed all posts list but while am display the post title in protected function render() then it shows post ID not a post name, I want to display post name and also want to get permalink of that post in Read More
$options = array();
$posts = get_posts( array(
'post_type' => 'digital_card'
) );
foreach ( $posts as $key => $post ) {
$options[$post->ID] = get_the_title($post->ID);
}
$this->add_control(
'post_name',
[
'label' => __( 'Select Posts', 'plugin-domain' ),
'label_block' => ('bool'),
'type' => \Elementor\Controls_Manager::SELECT,
'multiple' => true,
'options' => $options,
]
);
protected function render() {
$settings = $this->get_settings();
$show_title = $settings['post_name'];
?>
<?php echo $show_title; ?>
Read More
<?php
}
}
So you want to get all IDs and post_titles of all custom post_type 'digital_card'?! You don't need $key and get_the_title() to fetch the values for the options of control type Controls_Manager::SELECT2. You can set 'label_block' => true if you want the selection field in full width at the editor panel.
Part within protected function _register_controls():
$options = [];
$posts = get_posts( [
'post_type' => 'digital_card'
] );
foreach ( $posts as $post ) {
$options[ $post->ID ] = $post->post_title;
}
$this->add_control(
'posts',
[
'label' => __( 'Select Posts', 'your-plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'label_block' => true,
'multiple' => true,
'options' => $options,
]
);
So now the IDs of the selected posts will be saved in the control data posts.
To display the post_titles and a link to the related post, you fetch the values by the ID of the posts, which are stored in $settings['posts'] as an array.
Part within protected function render():
$settings = $this->get_settings();
$posts = $settings[ 'posts' ];
foreach( $posts as $post ) {
echo get_the_title( $post );
?>
Read More
<?php
}

WordPress: Add page to Yoast SEO breadcrumbs

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.

Wordpress Meta Box plugin, retrieving image url from custom post type

Having a bit of bother with the Wordpress Meta Box plugin, specifically with retrieving an image url from an image added to a custom post type.
I'm creating meta boxes in a custom plugin, like so:
add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );
function xxx_meta_register_meta_boxes( $meta_boxes )
{
$prefix = 'xxx_meta_';
$meta_boxes[] = array(
'title' => esc_html__( 'Retailer Information', '' ),
'id' => 'advanced',
'post_types' => array( 'xxx_retailers' ),
'autosave' => true,
'fields' => array(
// PLUPLOAD IMAGE UPLOAD (WP 3.3+)
array(
'name' => esc_html__( 'Retailer Logo', '' ),
'id' => "{$prefix}plupload",
'type' => 'plupload_image',
'max_file_uploads' => 1,
),
// URL
array(
'name' => esc_html__( 'Link', '' ),
'id' => "{$prefix}url",
'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
'type' => 'url',
'std' => 'xxx',
),
)
);
return $meta_boxes;
}
So far so good, these boxes are relevant to a custom post type 'xxx_retailers'.
The problem comes with retrieving this data. I want to display my retailers in a widget. I've chopped and changed another piece of code I've used previously, but it's not returning the image URL, just the ID. Unfortunately I don't know enough php to figure out why.
// Create Retailers Widget
// Create the widget
class Retailers_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'retailers_widget',
// name of the widget
__('XXX Retailers List', '' ),
// widget options
array (
'description' => __( 'Shows a list of retailer logos', '' )
)
);
}
function widget( $args, $instance ) {
// kick things off
extract( $args );
echo $before_widget;
echo $before_title . 'Retailers' . $after_title;
// Pull through Retailers
$xxxretailers = get_posts(array(
'post_type' => 'xxx_retailers',
'orderby' => 'title',
'order' => 'asc',
));
// Display for each Retailer
foreach ($xxxretailers as $xxxretailer) {
$custom = get_post_custom($xxxretailer->ID);
$meta_ret_img = $custom["xxx_meta_plupload"][0];
$meta_ret_url = $custom["xxx_meta_url"][0];
// Display Retailers
echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
}
}
};
// Register widget
function register_retailers_widget() {
register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );
The URLs are coming through correctly, so I know this is a problem with the line
$meta_ret_img = $custom["xxx_meta_plupload"][0];
But I can't figure out how to get the image URL from the data I presume is stored as an array. Any ideas?
Edit:
I should have mentioned, in a single post I can get a single image with:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
But I want to show images from all my retailers post types, to create a list of logos.
Replace this statement:
$meta_ret_img = $custom["xxx_meta_plupload"][0];
with this:
$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);
$meta_ret_img = $meta_ret_img_array[0];
also please remove all these curly braces from src and href attributes from your code.
if you are interested in any particular size of the image, then see the official document of wp_get_attachment_image_src() function here.
For e.g. for medium size image you can write it as:
wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');

WordPress Meta Box Plugin Displaying Cloned Text

I am trying to output this custom meta in one of my WordPress page templates. The documentation of the plugin seems to be lacking. ( http://metabox.io/docs/get-meta-value/ )
Because that I have clone as true it is displayed as so in the custom post type
I am trrying to display it VIA html so maybe the output would be something like
<ul>
<li>Red LED footwell lighting</li>
<li>Red LED Trunk Lighting</li>
<li>etc...</li>
</ul>
Here is how I defined the item I am trying to display
array(
'name' => 'Interior Mods',
'desc' => 'List all of the interior mods',
'id' => $prefix . 'intmods',
'type' => 'text',
'std' => '',
'class' => 'custom-class',
'clone' => true,
),
Thanks
You can use plug-in codes for get value.
$values = rwmb_meta(
'YOUR_PREFIX_text',
$args = array(
'type'=>'text',
// other options for meta field
),
$post_id = $post->ID
);
if($values){
echo "<ul>";
foreach ($values as $key => $value) {
echo "<li>".$value."</li>";
}
echo "</ul>";
}

How do I only display an image if it exists?

I am pretty new to php and having a little trouble figuring out what I am doing wrong. For some reason kd_mfi_the_featured_image( 'featured-image-3', 'post' ); dose not exists (even though It upload and looks fine in the word press admin panel) so it dose not = true and it will not display.
The goal is to display featured-image-3 of all the child pages only if featured-image-3 exists ( I am using this plugin to have multiple featured images). My method is as follows
Get current page ID
Us current page ID to query child pages
If there are child pages setup post data for each child page
Check to see if the child page has featured-image-3
If child page has featured_image-3 display it : If not query next post
Code in customPage.php
<?php if ( have_posts() ) { /* Query and display the parent. */
while ( have_posts() ) {
the_post();
the_content();
$thispage=$post->ID;
}
} ?>
<?php $childpages = query_posts('post_type=page&post_parent='.$thispage);
if($childpages) /* display the children content */
foreach ($childpages as $post) :
setup_postdata($post);
if (kd_mfi_the_featured_image( 'featured-image-3', 'post' )) {
kd_mfi_the_featured_image( 'featured-image-3', 'post' );
the_post_thumbnail();
}
else {
$wp_query->next_post();
}
endforeach; ?>
Code in Functions.php
if( class_exists( 'kdMultipleFeaturedImages' ) ) {
$args2 = array(
'id' => 'featured-image-2',
'post_type' => 'page', // Set this to post or page
'labels' => array(
'name' => 'Home Page Carousel',
'set' => 'Set Home Page Carousel',
'remove' => 'Remove Home Page Carousel',
'use' => 'Use as Home Page Carousel',
)
);
$args3 = array(
'id' => 'featured-image-3',
'post_type' => 'page', // Set this to post or page
'labels' => array(
'name' => 'Product Category Hero',
'set' => 'Set Product Category Hero',
'remove' => 'Remove Product Category Hero',
'use' => 'Use as Product Category Hero',
)
);
$args4 = array(
'id' => 'featured-image-4',
'post_type' => 'page', // Set this to post or page
'labels' => array(
'name' => 'Product Category Featured',
'set' => 'Set Product Category Featured',
'remove' => 'Remove Product Category Featured',
'use' => 'Use as Product Category Featured',
)
);
new kdMultipleFeaturedImages( $args2);
new kdMultipleFeaturedImages( $args3 );
new kdMultipleFeaturedImages( $args4 );
}

Categories