How to Call Name of WordPress Site?
-
Basically with PHP I want to call the name of the site. What code do I use?
I’m thinking:
‘title’ => __( “{$show[‘name’]} is your site name” ),
or
‘title’ => __( “$name is your site name” ),
but they don’t work. What code would work?
Thanks!
-
If you mean display the name of your blog then try using
bloginfo('name');
https://codex.www.ads-software.com/Function_Reference/bloginfo
Edit: Or perhaps this, since
get_bloginfo()
might be more to what you are looking to do.$blog_title = get_bloginfo('name');
https://codex.www.ads-software.com/Function_Reference/get_bloginfo
When using this code:
if ( is_admin() ) { // Add an option to visit the site. $wp_admin_bar->add_menu( array( 'parent' => 'site-name', 'id' => 'view-site', 'title' => __( "bloginfo('name') is your site" ), 'href' => home_url( '/' ), ) );
I’m just getting:
bloginfo(‘name’) is your site
How do I get the actual site name?
Try
get_bloginfo('name')
instead.'title' => __( get_bloginfo('name') . ' is your site' ),
First off I appreciate you trying to help me.
However after adding the code I’m not getting anything except:
is your site
Not
SITENAME is your site
After wrapping them in double quotes I’m getting the literal code instead.
You’re trying to add that to your WordPress dashboard or for display on your site? I’m asking because that function does work in the header but I’m not sure it would work in the admin pages.
Also are you on a regular WordPress installation or a multisite?
Yes, I’m trying to change the admin bar text. I’m running on a regular installation.
After wrapping them in double quotes I’m getting the literal code instead.
Yes because in quotes it goes from being a PHP function to just plain text.
I’m sure this is possible, just need to know how. ??
*Looks at lines 230-232 in
wp-includes/admin-bar.php
*This ought to work…
$title = wp_html_excerpt( $blogname, 40 ); if ( $title != $blogname ) $title = trim( $title ) . '…';
So try and see if this works.
$title = wp_html_excerpt( $blogname, 40 ); if ( $title != $blogname ) $title = trim( $title ) . '…'; if ( is_admin() ) { // Add an option to visit the site. $wp_admin_bar->add_menu( array( 'parent' => 'site-name', 'id' => 'view-site', 'title' => __( $title . ' is your site' ), 'href' => home_url( '/' ), ) );
Or at least if it displays anything in the title area.
The code is showing:
mysite.com is your site
and what I wanted was:
Mysite is your site
(the actual site name, not the URL)
Thanks a lot anyways though, I really do appreciate your time to help me. Hopefully this can get solved!
The code is showing:
mysite.com is your site
OK! That’s an improvement. It just means that
$blogname
isn’t quite right.I’m really looking at this the wrong way meaning there’s probably a much easier way to do this.
From line 227 of that same file try inserting this line above the code I’ve posted.
$blogname = sprintf( __('Global Dashboard: %s'), esc_html( $current_site->site_name ) );
See if that gets you the
site_name
properly.I placed the code right above this one:
if ( is_admin() ) { // Add an option to visit the site. $wp_admin_bar->add_menu( array( 'parent' => 'site-name', 'id' => 'view-site', 'title' => __( $title . ' is your site' ), 'href' => home_url( '/' ), ) );
and it’s still displaying “https://mysite.com is your site”
At least we have improvement. ??
I think I got it though I had to make some changes. I was over thinking the problem.
It’s
get_bloginfo( 'name' )
that worked for me. Also I changed theview-site
id for the menu tomh-menu
.I tested this in a small plugin but this function should work for you.
<?php /* Plugin Name: Adding the menu option to visit the site */ add_action( 'admin_bar_menu', 'mh_admin_menu', 10 ); function mh_admin_menu () { global $wp_admin_bar; if ( is_admin() ) { // Add an option to visit the site. $wp_admin_bar->add_menu( array( 'parent' => 'site-name', 'id' => 'mh-menu', 'title' => __( get_bloginfo('name') . ' is your site' ), 'href' => home_url( '/' ), ) ); } }
I was hoping I could just add that to the adminbar.php file and avoid creating a function.
The result of that was “is your site”. ??
I was hoping I could just add that to the adminbar.php file and avoid creating a function.
Wait. What?? Do you mean modify
wp-includes/admin-bar.php
?? No don’t do that. Really don’t.The toolbar is designed to be easily modifiable. You can add items, remove existing menu nodes, etc. It’s so easy to do that even an amateur like myself can do it and there is nothing wrong with adding that function via a plugin or your theme’s
functions.php
file. Doing that is trivial.But when you start hacking the core WordPress files then A) you will lose your work when you update and B) you may make your system unstable and prone to exploits.
It’s so easy to modify the menu the right way, please don’t try and go about it the wrong way.
Here’s a post that can show you how you can really manipulate that menu anyway you’d like to.
https://halfelf.org/2011/customize-wp-admin-bar/
Before I spent the ~10 minutes or so making that small plugin I read that post. You can really go nuts and modify the menu 10 different ways. It’s easy when you get the hang of it.
- The topic ‘How to Call Name of WordPress Site?’ is closed to new replies.