Change Logo by User Role in Wordpress - php

Hey guys I have been asked to brand the site for my company. We are using Wordpress for it and basically each User Role is a different group that needs to see a different logo on the site.
I am using Eduma by Thimpress as my theme. I feel like it should be as simple as checking what user_role is logged in and changed the logo image, but I don't know where to start to put that condition.
Any help or guidance is much appreciated!
Below is the code I think creates the logo:
<?php
add_action( 'thim_logo', 'thim_logo', 1 );
// logo
if ( !function_exists( 'thim_logo' ) ) :
function thim_logo() {
$thim_logo = get_theme_mod( 'thim_logo', false );
$style = '';
if ( !empty( $thim_logo ) ) {
if ( is_numeric( $thim_logo ) ) {
$logo_attachment = wp_get_attachment_image_src( $thim_logo, 'full' );
if ( $logo_attachment ) {
$src = $logo_attachment[0];
$style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
} else {
// Default image
// Case: image ID from demo data
$src = get_template_directory_uri() . '/images/logo.png';
$style = 'width="153" height="40"';
}
} else {
$src = $thim_logo;
}
} else {
// Default image
// Case: The first install
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
$src = thim_ssl_secure_url($src);
echo '<a href="' . esc_url( home_url( '/' ) ) . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . ' - ' . esc_attr( get_bloginfo( 'description' ) ) . '" rel="home" class="no-sticky-logo">';
echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
echo '</a>';
}
endif;
add_action( 'thim_sticky_logo', 'thim_sticky_logo', 1 );
// get sticky logo
if ( !function_exists( 'thim_sticky_logo' ) ) :
function thim_sticky_logo() {
$sticky_logo = get_theme_mod( 'thim_sticky_logo', false );
$style = '';
if ( !empty( $sticky_logo ) ) {
if ( is_numeric( $sticky_logo ) ) {
$logo_attachment = wp_get_attachment_image_src( $sticky_logo, 'full' );
if ( $logo_attachment ) {
$src = $logo_attachment[0];
$style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
} else {
// Default image
// Case: image ID from demo data
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
} else {
$src = $sticky_logo;
}
} else {
// Default image
// Case: The first install
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
$src = thim_ssl_secure_url($src);
echo '<a href="' . esc_url( home_url( '/' ) ) . '" rel="home" class="sticky-logo">';
echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
echo '</a>';
}
endif;

In the file where you set the logo put this code there.
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
//logo for author role
}

That depends on the layout of the theme. Likely, it's in the header.php.
The way to proceed is something like this:
Create a child theme
https://codex.wordpress.org/Child_Themes
Find out what file outputs the link to the logo
Probably header.php but that depends on the theme; ask the theme developer if unsure.
Use for example wp_get_current_user()
https://codex.wordpress.org/Function_Reference/wp_get_current_user
Do a php switch on the outcome of that and output various links
http://php.net/manual/en/control-structures.switch.php

Related

Making custom field appear in captions for image attachments (Wordpress)

I'm a designer (not a developer) working on a Wordpress site for a customer. Using the ACF-plugin I've set up a custom field on media files for photo credits. This works fine on featured images, where I can call it in single.php like this:
$post_thumbnail = get_post(get_post_thumbnail_id());
$credit = get_field('media_credit', $post_thumbnail);
if($credit):
echo '<div class="media-credit"><p>Photo: '.$credit.'</p></div>';
endif;
So I know the custom field works, and outputs the right data. However, I can't get it to work on image attachments in posts. What I have is this:
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
//OUTPUT CREDIT
$photographer = get_field( 'media_credit', $attachment_id );
if ($photographer):$media_byline = '<br/><span class="media-credit">Photo: '.$photographer.'</span>';endif;
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
If I remove the if-statement in OUTPUT it shows «Photo: » within the captions, after the text like it should, but it doesn't get any data. What am I missing?
(BTW – I know there are plugins that outputs image credits, but they tend to have styles and features I have to override, resulting in a spaghetti mess I'd hate to hand over to the next guy working on this site.)
Finally got this to work! :-D Instead of using $attachment_id, I got the ID from $attr, and then stripped the 'attachment_' prefix from output.
I also made separate fields for photographer and bureau, but I guess that's beside the point.
Here is the code:
function my_img_caption_shortcode( $empty, $attr, $content ){
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
$credit_id = $attr['id'];
$credit_id = str_replace( 'attachment_', '', $credit_id );
$photographer = get_field( 'media_credit', $credit_id );
$bureau_credit = get_field( 'media_bureau', $credit_id );
if ( $photographer && $bureau_credit ): $dash = ' / ';
endif;
if ( $photographer || $bureau_credit ): $media_byline = '<br/><span class="media-credit">PHOTO: '
. $photographer . ''
. $dash . '<span class="bureau-credit">'
. $bureau_credit
. '</span></span>';
endif;
return '<div id="attachment_' . $credit_id . '"'
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '' . $media_byline . '</p>'
. '</div>';
}
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
This solution is something I lifted from the AFC Media Credit-plugin, so credits to the developer.
I hope this is useful for anybody who wants to achieve something similar.

Display the product category thumbnail on Woocommerce product page

I would like to add on my product the image of the category parent, I manage to recover the description of my category
but not the picture
Any help is highly appreciated.
The following function based on woocommerce_subcategory_thumbnail() source code will display any product category thumbnail from category Id, name or slug:
/**
* Display product category thumbnail.
*
* #param mixed $product_category Category term Id, term name or term slug.
*/
function display_product_category_thumbnail( $product_category ) {
$taxonomy = 'product_cat';
if( term_exists( $product_category, $taxonomy ) ) {
if( is_numeric($product_category) )
$field_type = 'term_id';
else
$field_type = 'slug';
} else
return;
$term = get_term_by( $field_type, sanitize_title( $product_category ), 'product_cat' );
$small_thumb_size = 'woocommerce_thumbnail';
$dimensions = wc_get_image_size( $small_thumb_size );
if ( $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumb_size );
$image = $image[0];
$image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumb_size ) : false;
$image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumb_size ) : false;
} else {
$image = wc_placeholder_img_src();
$image_srcset = false;
$image_sizes = false;
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds.
// Ref: https://core.trac.wordpress.org/ticket/23605.
$image = str_replace( ' ', '%20', $image );
// Add responsive image markup if available.
if ( $image_srcset && $image_sizes ) {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $term->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
} else {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $term->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
}
}
}
Tested and works
Update - Addition (Related to your comments)
To get the product categories from the WC_Product object $product and display it with its description and thumbnail, you will use:
global $product;
$term_ids = $product->get_category_ids();
// Loop through product category IDs
foreach( $term_ids as $term_id ){
$term = get_term ( $term_id, 'product_cat' );
// Product category name
echo '<p>' . $term->name . '</p>';
// Product category description
echo '<p>' . $term->description . '</p>';
// Product category description
display_product_category_thumbnail( $term_id )
}
Tested and works
Note: $product->get_categories(); is outdated and deprecated and gives a list of formatted product categories, which is not really the best effective way.
For info: get_categories() method is replaced by function wc_get_product_category_list()

Permalink of specific post ID

Below the code I have on my page tot display the permalink of q specific post in Wordpress. It works, but I have the feeling it can be easier. Can somebody explain how?
$post_id = 26; // post id
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$content = $queried_post->post_content;
$perma = get_permalink($post_id);
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
}
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo $content;
Well your code seems ok. However there are some improvements that can be made.
For example, when you have the $queried_post object, you don't need to create additional variables for content and title. You can use this object properties to get the values.
Also you can use wordpress get_the_post_thumbnail to show featured image.
Some formatting and it's almost perfect.
$post_id = 26; // post id
$queried_post = get_post($post_id);
echo '<a href="' . get_permalink( $post_id ) . '" title="' . $queried_post->post_title . '">';
echo $queried_post->post_title;
echo '</a>';
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail( $post_id, 'full', array('width' => '100%') );
}
echo $queried_post->post_content;
try this code
$post_id = 26;
if ( has_post_thumbnail($post_id) )
{
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id),’thumbnail’ );
}
echo '<a href="' . get_the_permalink($post_id) . '" title="' .get_the_title($post_id). '">';
echo get_the_title($post_id);
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo get_the_content($post_id);
In response to your comment, if you want to simplify this line:
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
You could do:
echo '' . $title . '';
So replacing multiple echo; with the "." to 'continue' without breaks.

Get image caption in Wordpress

I have this code to retrieve the image url for the images in a gallery and it works ok but i cannot figure out how i could get the caption for each image.
I tried searching all over but i cannot seem to put all the information out there together! Any suggestions on how i could retrieve the captions?
function show_related_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = <<<END
<div class="side_bar">
<div class="related">
<h3>Related Images</h3>
END;
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$src = $image;
$image_list .= '<a href="' . $src . '" rel="' . get_the_title() . '">'
. '<img src="' . $src .'" />'
. '</a>';
}
}
$image_list .= '</div></div>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'show_related_gallery_image_urls' );
I hope i explained myself well! Thanks!
This hasn't been tested by try it, I cleaned up some of your code a bit:
1) Combined the first 2 IF statements into 1
2) Used get_post_gallery() (Codex) which returns the src and the image ID. We use the image ID to return the caption, we can also get the description and more if we needed to.
3) Removed the containing Foreach Statement since both my method only returns 1 gallery, not multiple so no need to loop through.
function show_related_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() || !has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_gallery( $post, false );
$image_list = <<<END
<div class="side_bar">
<div class="related">
<h3>Related Images</h3>
END;
// Loop through each image in each gallery
$i = 0; // Iterator
foreach( $gallery['src'] as $src ) {
$caption = wp_get_attachment($gallery['id'][$i])['caption'];
$image_list .= '<a href="' . $src . '" rel="' . get_the_title() . '">'
. '<img src="' . $src .'" />'
. '<div class="caption">'
. $caption
. '</div>'
. '</a>';
$i++; // Incremenet Interator
}
$image_list .= '</div></div>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'show_related_gallery_image_urls' );
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
On a sidenote, there is a gallery filter function where you can change how the gallery is displayed post_gallery Filter, here's a question that kind of shows how to edit it. There's also a great WordPress Stack Exchange where that may be helpful in the future!

Direct a portfolio item to the single project and disable the ajax script wordpress theme

At the moment on my portfolio page you can click on a portfolio item and a ajax container pops up. And in this container you can click on a button to go to the project and read more details.
BUT
I want to disable this ajax container and when you click on a single portfolio item it needs to go straight to the project item page with all the details on it etc.
Now have I been searching in all of the .php files and in the scripts but I just can't find the action when you click on a portfolio item and I'm not really sure with what I need to replace it when found. I hope someone could help me with this one.
Here is the code of the portfolio page:
<div id="portfolio-grid" class="clearfix">
<?php
while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
get_template_part( 'includes/entry', 'portfolio' );
endwhile;
wp_reset_postdata();
?>
</div> <!-- end #portfolio-grid -->
And here is the code of the Ajax container -I think-
function et_show_ajax_project(){
global $wp_embed;
$project_id = (int) $_POST['et_project_id'];
$portfolio_args = array(
'post_type' => 'project',
'p' => $project_id
);
$portfolio_query = new WP_Query( apply_filters( 'et_ajax_portfolio_args', $portfolio_args ) );
while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
global $post;
$width = apply_filters( 'et_ajax_media_width', 600 );
$height = apply_filters( 'et_ajax_media_height', 480 );
$media = get_post_meta( $post->ID, '_et_used_images', true );
echo '<div class="et_media">';
if ( $media ){
echo '<div class="flexslider"><ul class="slides">';
foreach( (array) $media as $et_media ){
echo '<li class="slide">';
if ( is_numeric( $et_media ) ) {
$et_fullimage_array = wp_get_attachment_image_src( $et_media, 'full' );
if ( $et_fullimage_array ){
$et_fullimage = $et_fullimage_array[0];
echo '<img src="' . esc_url( et_new_thumb_resize( et_multisite_thumbnail($et_fullimage ), $width, $height, '', true ) ) . '" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" />';
}
} else {
$video_embed = $wp_embed->shortcode( '', $et_media );
$video_embed = preg_replace('/<embed /','<embed wmode="transparent" ',$video_embed);
$video_embed = preg_replace('/<\/object>/','<param name="wmode" value="transparent" /></object>',$video_embed);
$video_embed = preg_replace("/height=\"[0-9]*\"/", "height={$height}", $video_embed);
$video_embed = preg_replace("/width=\"[0-9]*\"/", "width={$width}", $video_embed);
echo $video_embed;
}
echo '</li>';
}
echo '</ul></div>';
} else {
$thumb = '';
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Ajaximage');
$thumb = $thumbnail["thumb"];
echo '<a href="'. esc_url( get_permalink() ) . '">';
print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext);
echo '</a>';
}
echo '</div> <!-- end .et_media -->';
echo '<div class="et_media_description">' .
'<h2 class="title">' . '' . get_the_title() . '' . '</h2>' .
truncate_post( 560, false );
echo '</div> <!-- end .et_media_description -->';
echo '<a class="more" href="' . get_permalink() . '">' . __( 'Meer info »', 'Flexible' ) . '</a>';
endwhile;
wp_reset_postdata();
die();
}
You can see the page here: http://bit.ly/10BDVVf
Again thank you!
This is being controlled via javascript in /wp-content/themes/Flexible/js/custom.js. Comment out lines 29-77 and it will link as expected (the return false at the end cancels that standard navigation), but keep in mind that if you edit the theme directly and upgrade it, your changes will be overwritten.

Categories