I am making a wordpress site now, and I just inserted an HTML block into my sidebar. I want to to serve 2 functions.
1. If the user isn't signed in, I want it to display a login block. (working)
2. If the user is signed in, I want it to display their avatar. (not working)
I looked up the get_avatar function on the wordpress site, and this is what I found. But it doesn't show anything...
My code:
[logged_out][login][/logged_out]
[logged_in]
<?php
global $current_user;
get_currentuserinfo();
echo get_avatar(ID, 128 );
?>
[/logged_in]
My question, is this because it is an HTML block, and it won't support PHP, or is something wrong with the code?
Define a shortcode in your functions.php
http://codex.wordpress.org/Shortcode_API
Return <img src="avatar_src" /> from shortcode definition
Then you can use that shortcode in your text widget and add this filter to make it working
add_filter('widget_text', 'do_shortcode');
http://codex.wordpress.org/Function_Reference/do_shortcode
Related
I'm making a WordPress theme by myself since I'm working for the first time in Wordpress I've watched some tutorials about it.
I have page.php header and footer and ofc an index. I insert the content from the pages with this:
<?php echo get_post_field('post_content', $post->ID); ?>
but I tried the get_post in a while loop with same result..
Everything is fine but when I want to use a plugin I can't add to my page... When I insert the shortcode of it it shows only the shortcode string... There are some plugins where I can click a "view" option and it would show a page with my plugin (for example a calendar) but that page is empty...
When I activate an original theme it works instantly... So I'm sure something is missing from my theme something which can load the plugins but I couldn't find solution for it.
Any ideas?
Did you add the <?php wp_head(); ?> function before the head area of the html document is closing? It imports important scripts and styles from wordpress itself (and probably also from the plugins).
See here:
https://developer.wordpress.org/reference/functions/wp_head/
Before closing the body area, the template should also include
<?php wp_footer();?>
See here:
https://developer.wordpress.org/reference/functions/wp_footer/
I'm using shortcodes in my Wordpress page to display a responsive Google Map centered at specific location. To do it I'm using shortcode supported by external Wordpress plugin. Such shortcode is let's say [map address="New York"].
As all my Wordpress posts titles are names of the cities, I would like to automatize the process and be able to display the titles of the post in place of the address parameter in [map] shortcode.
Is it possible? Is there any way to do it? As far as I know nesting code or variables in shortcode isn't supported by Wordpress but maybe there is some kind of a workaround.
The same behavior I'd like to accomplish for 'custom field' - really similar situation.
I'd appreciate any help and any advice
Try to put this code to functions.php file in your theme:
function mymap_shortcode() {
echo do_shortcode('[map address="'.esc_html( get_the_title() ).'"]');
}
function mymap_shortcodes_init() {
add_shortcode('mymap', 'mymap_shortcode');
}
add_action('init', 'mymap_shortcodes_init');
And then in posts/pages (or even put it directly to template, or create another filter to add that) use shortcode [mymap] that will run [map] shortcode with current page title as parameter.
I want to hide a text-widget in wordpress using conditional tags when users are logged in. I've learned that using
is_user_logged_in()
shows a text widget when users are logged in.
If you're comfortable changing the code where the widget is outputted you could use the is_user_logged_in() function. Something like this:
<div id="widget_area">
<?php
if ( is_user_logged_in() ) {
// show nothing
} else {
dynamic_sidebar('widget_name');
}
?>
</div>
The downside being that this is now hard coded and you might have to add this function in a few different files.
So, I'll recommend to use Widget Logic plugin. Widget Logic is a very popular plugin that will help you accomplish your goal. While Widget Logic can help you do much more than hide or show a widget to logged in/logged out users.
Once installed and activated, you will see an empty box at the bottom of each widget. (Appearance > Widgets)
I am making a custom theme with shortcodes. The theme is ready, but now I need to make the shortcodes.
The problem is that the shortcodes don't work in my posts, they are only visible on my pages. When I add a shortcode in my post, the post displays nothing.
Here is the shortcode code:
function rss_subscribe_link() {
return 'Enjoy what you\'re reading? Subscribe to our RSS feed here now.'; }
add_shortcode('rss-subscribe', 'rss_subscribe_link');
add_filter('widget_text', 'do_shortcode');
Does anyone know how to solve this problem?
Greets,
Joren
This could happen if your post content is displayed via get_the_content() which is a function that returns the content without applying the default filters (wpautop, do_shortcode etc) that are applied normally when you use the_content(). Could you confirm if this is the case?
I am creating a custom members area for my client's employees. Basically, what I've done so far is I created a new role=consultants and I gave that role a read only access.
Then I uploaded the Peter's Login Redirect Plugin so that the consultants (employees) land in a page called CONSULTANTS PORTAL. From there, they will be able to access their individual page which it will load as long as the name of the page matches the username given to them. That way they can only see their own page.
To see this process, you can visit this link in the wordpress.org forums EASY CLIENT PORTAL
So I've managed a lot of it, except...I am supposed to duplicate the page.php and then add the script that will make the individual page show up. But, the Genesis Framework is pretty complicated. The page.php has an empty skeleton and the actual meat of the page is in a li/structure root folder (That's what I think anyway) .
As it is right now, I have the following in my default template page consultants-portal.php
<?php
/**
* Template Name: Consultants Portal
*/
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
genesis();
?>
This code gets me this. You can see the content (my page) loading before the page loads. Which tells me there is something else I need to add to this so that the content loads in the actual white area of the page.
The instructions in the link I mentioned says to add the dynamic script right above the is_page or have_posts, but I as I said, Genesis doesn't have this in page.php. instead it is all broken in pieces and spread through the root.
Sorry if I made this too long to read, I wanted you to have all the info I have.
has anyone done this before?
Try out the following code:
<?php
/**
* Template Name: Consultants Portal
*/
// remove Genesis default loop
remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
// add a custom loop
add_action( ‘genesis_loop’, ‘my_custom_loop’ );
function my_custom_loop () {
// add your queries or custom structure here
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
}
genesis(); ?>
Instead of writing the code directly, write it inside the loop function as above.