Admin tool bar in wordpress - php

I am developing my own wordpress theme for very first time. I want that when admin login to WordPress, at top admin tool bar must be show on main front end of website.
I tried following things
if (is_user_logged_in())
{
show_admin_bar(true);
}#end if
in functions.php
What I believe that I missed some thing in header.php or index.php, but I am not sure.

The proper way to do this is with a filter in functions.php:
function my_function_admin_bar(){
return is_user_logged_in();
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');
The admin bar is called as part of the wp_footer() function, so you need to make sure you call that function in your footer section of the template:
<?php
wp_footer();
?>
A discussion of some specific issues that can cause this to break can be found here:
http://wordpress.org/support/topic/admin-bar-not-displaying
And finally, more details on how to use the show_admin_bar() in the functions.php file can be found here:
http://codex.wordpress.org/Plugin_API/Filter_Reference/show_admin_bar

Related

How to add plugins to my custom wordpress theme?

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 am new to WordPress but I cant seem to add php code?

So I wrote a script which checks if there is a session with a certain name or not and echoes hello world but after switching to WordPress I wasn't able to use any of my old scripts. Whenever I type them it thinks they are text and just prints it. So I tried a couple of stuff like a plugin that I could add my script in and it produces a shortcode that I can use. I tried that but it didn't worked. I can't find what is happening.
Are there some sort of logs I can refer to? If someone knows the fix please help (I'll leave the code down below). result-id session is a msg and result session is another message.
<?php
if(isset($_SESSION['result-id'])){
echo("hello world");
else if(isset($_SESSION['result'])){
echo $_SESSION['result'];
}
session_unset();
?>
To start using sessions in WordPress you need to start it using an action hook:
function start_wp_session(){
if( !session_id() )
session_start();
}
add_action('init','start_wp_session');
If you want to custom code in WordPress, first you must create a child-theme of your currently active theme. Then in the function .php file of your child theme folder, you can write any of your code. But you cannot start writing directly in function.php file. You must bind your code in a custom function, and then hook that function with any hooks present in the WordPress.
Example hooks- wp_header, wp_footer, init, etc.
See the example below.
function custom_function(){
echo "Hello World!";
}
add_action('wp_head','custom_function');
You can wrap any code inside the custom function.

Can't find where the do_action("This function") leads to in wordpress theme

I have this in my index.php file. It adds the home banner image in WordPress. I know that it is mostly generated in WordPress customizer, but I need to add an anchor tag in this section. I can't find it anywhere in the file structure.
<?php do_action('cleanblog_index_top'); ?>
I'm not able to find where cleanblog_index_top leads to. Any help would be great. Thank you!
I stumbled on this old one while looking up the docs for do_action(). The answers are brutal so I decided to provide a better answer in case anyone else stumbles here.
If a WordPress theme has something like do_action( 'example_action_hook_tag' ) somewhere in one of the template files (such as index.php, page.php or whatever) the purpose is to provide theme or plugin authors with a way to write their own custom function that they can then "hook" onto the action with the function add_action().
WordPress would then call this function any time and anywhere do_action( 'example_action_hook_tag' ) is called.
The creators of commercial themes will often litter their template files with hooks declared with do_action() to make it easier for their customers to customize their themes via functions.php or by writing a site-specific plugin.
It looks to me that this is the likely scenario that is impacting the OP. This also explains why the OP was unsuccessful in finding where this "leads to".
It would only "lead somewhere" if the OP wrote a function in the theme/child-theme functions.php or in a plugin and added the line do_action( 'cleanblog_index_top', 'name_of_ops_function' ) to hook their function onto the cleanblog_index_top. WordPress would then call their function when do_action( 'cleanblog_index_top' ) was called in index.php.
From the name of the OP's hook, cleanblog_index_top, it sounds like the theme author intended to provide a way for others to inject output at the top of the index page template.
Suppose the OP wanted <h1>Hello World</h1> to appear there.
In functions.php of a theme/child-theme the OP could add a function that echo's this out:
function op_customization() {
echo '<h1>Hello World</h1>';
}
And hook their function onto cleanblog_index_top:
add_action( 'cleanblog_index_top', 'op_customization' );
Cheers!
You should never edit the index.php file directly, for the same reason you should never edit core Wordpress files directly - the next time WP pushes an update, your changes will be overwritten (and that assumes you don't break anything). Never edit plugin files directly, same reason.
You need to look in your theme, you should only make changes to the functions.php and style.css files in your theme - unless you create a child theme and that is a topic you should Google.

Redux Framework in Wordpress not working

I don't know what is wrong with me. I just simple generated a file using build.reduxframework.com and then added in my Wordpress theme. Then added the following line to functions.php
if(is_admin()){
require_once(get_template_directory().'/core/admin/admin-init.php');
}
Good thing is the demo stuff is showing up. Now my $opt_name is for_test. In my header.php I am adding the following code.
<h2><?php
global $for_test;
echo $for_test['textarea-example'];
?></h2>
PS: textarea-example is the id of the field which is already added in the files I downloaded. But I don't know why it is not fetching the value in it.
is_admin() means that only admins will load that. So if you're logged out, your redux config will never load.
Best to just include it. Redux will take care of when it should run.

WordPress : How to add custom template in genesis framework

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.

Categories