I created my custom plugin and want to add custom logo, but somehow it keeps redirecting me to wrong dir, this is error I get:
GET http://localhost/logo.png 404 (Not Found)
Here is what I tried so far:
$plugin_dir = str_replace( $base_url, ABSPATH, $plugins_url );
$plugin_dir = plugins_url();
$plugin_dir = plugin_dir_path( __FILE__ );
$plugin_dir = WP_CONTENT_DIR . '/plugins';
$plugin_dir = plugins_url( '/', __FILE__ );
Here is my add action:
function kalbos_modifymenu() {
add_menu_page('Kalbos', //page title
'Kalbos', //menu title
'manage_options', //capabilities
'kalbos_list', //menu slug
'kalbos_list', //function
$plugin_dir . 'logo.png',
'5'
);
}
add_action('admin_menu','kalbos_modifymenu');
However icon is working when i move my logo to wp-admin/logo.png dir because thats where my logo path goes even if i set it go to my plugin folder
plugins_url will (my emphasis):
Retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.
So, the following will point exactly to your plugin directory with an ending slash:
$plugin_dir = plugins_url( '/', __FILE__ );
// results in http://localhost/wp-content/plugins/YOUR_PLUGIN/
And to target the logo, use:
$plugin_dir . 'logo.png'
Another example, to make a shortcut to your images folder:
$plugin_img = plugins_url( '/images/', __FILE__ );
// results in http://localhost/wp-content/plugins/YOUR_PLUGIN/images/
Try to add an other slash to /plugins like this: /plugins/ and also close your quotes on the same line. At the moment you have it like 'plugins; but the correct is '/plugins';
Related
I'm NOT a WordPress theme author! I want to load .json translations that comes from related admin .js string files inside a WordPress theme. I succeed to load translations by the following function:.
add_action( 'admin_enqueue_scripts', 'kadence_child_js_translations' );
function kadence_child_js_translations() {
if ( get_locale() == 'fa_IR' ) {
$handle = 'my-kadence-customizer-js';
$js_uri = get_template_directory_uri() . '/inc/dashboard/react/src/customizer.js';
$domain = 'kadence';
$json_path = get_stylesheet_directory() . '/languages';
wp_register_script( $handle, $js_uri );
wp_enqueue_script( $handle );
wp_set_script_translations( $handle, $domain, $json_path );
}
}
When I refresh the admin panel, Translations are loaded But I get this error in console:
Uncaught SyntaxError: import declarations may only appear at top level of a module ( customizer.js:6 )
I tried to use the load_script_textdomain() function instead of wp_set_script_translations() but not only none of translations are loaded but also I have this error again!
How can I load .json scripts of a WordPress Theme from a child theme correctly?
In WordPress, I have a template file called vertical-page.php.
When a page has this template applied, I want to load in a specific stylesheet and script.
I have tried to use is_page_template(), but it doesn't work. When I view the source of the page, the script and styles are not being loaded?
<?php
add_action('wp_enqueue_scripts', 'theme_scripts');
function theme_scripts() {
wp_enqueue_style('bootstrap-grid-style', get_template_directory_uri() . '/assets/css/bootstrap-grid.css', array(), STYLE_VERSION);
if ( is_page_template( 'vertical-page' ) ) {
wp_enqueue_style('tmp-override', get_template_directory_uri() . '/assets/css/override.css', array(), STYLE_VERSION);
wp_enqueue_script('overrides-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery'),STYLE_VERSION, true);
}
?>
Try putting the add_action after the function
If your page template file is something.php and it's location is the main folder of the active theme, you need to pass an argument to the is_page_template() function like this: is_page_template('something.php')
I am writing a some code for wordpress theme development that I plan on reusing for future themes (maybe even uploading to github).
it consist of a few dozen files and some javascript and css files as well.
the only commitment I am willing to make for the future is that all my files will be placed in the same directory, where will this directory be placed inside the theme directory is unknown.
how should I go about enqueuing files (wp_enqueue_style, wp_enqueue_script functions) if I don't know the files absolute path (get_template_directory_uri . '')?
also I hope that instead of having a dozen lines of include\require, I can write one include file that will include the rest of the files by their relative paths.
A simple solution, (not necessarily the most proper way) if you're needing the path within the theme index file could be something like so:
<?php
$TEMPLATE_PATH = get_template_directory_uri();
$TEMPLATE_PATH = parse_url($TEMPLATE_PATH, PHP_URL_PATH);
?>
You'd then be able to use the $TEMPLATE_PATH as a relative path like so:
<link href="<?php echo $TEMPLATE_PATH; ?>/favicon.ico" rel="shortcut icon" type="image/x-icon"/>
this would be output like the following:
<link href="/wp-content/themes/ThemesName/favicon.ico" rel="shortcut icon" type="image/x-icon"/>
Hope someone finds this useful.
I ended up using dirname(__FILE__) to determine loader.php location,
and substracting get_template_directory() out of it to get the relative path of my code inside the theme directory like this:
$MY_PATH = str_replace(realpath(get_template_directory()),"",dirname(__FILE__)));
end result load.php:
$MY_PATH = str_replace(realpath(get_template_directory()),"",dirname(__FILE__)));
require_once('file1.php');
require_once('file2.php');
require_once('file3.php');
function my_scripts() {
$mypath = $GLOBALS['MY_PATH'];
wp_enqueue_style( 'my-style', $template_directory_uri . $mypath . '/style.css');
wp_enqueue_script( 'my-js', $template_directory_uri . $mypath . '/script.js');
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );
now I can change my code directory location without editing the code in loader.php.
There are two main WP functions to use to find the relative paths within a theme:
get_template_directory()
get_template_directory_uri()
You could create a theme subdirectory called 'reusable' or something appropriate, which is a top level subdirectory. Furthermore, let's say your set of files are as follows:
mytheme/reusable/loader.php
mytheme/reusable/include-me.php
mytheme/reusable/include-another.php
mytheme/reusable/js/reuse.js
mytheme/reusable/css/reuse.css
loader.php:
<?php
// add as many files as you want to this array
$include_paths = array(
'reusable/include-me.php',
'reusable/include-another.php',
);
// loop through the paths and include them if they exist
$template_directory = trailingslashit(get_template_directory());
foreach( $include_paths as $path ) {
$complete_path = $template_directory . $path;
if( file_exists( $complete_path ) ) {
include($complete_path);
}
}
// function to enqueue js and css
function reusable_enqueue_scripts() {
$template_directory_uri = get_template_directory_uri();
wp_enqueue_style( 'reusable-style', $template_directory_uri . '/css/reuse.css');
wp_enqueue_script( 'reusable-js', $template_directory_uri . '/js/reuse.js');
}
// add function to enqueue action hook
add_action( 'wp_enqueue_scripts', 'reusable_enqueue_scripts' );
Then in your theme's functions.php file:
$reusable = trailingslashit(get_template_directory()).'reusable/loader.php';
include($reusable);
There's also a very useful WP function called get_template_part() that you can look into, might change how you're thinking about a solution.
you can retrieve theme directory URI from below WP function , you can then pass files name with respective folder name.
get_template_directory_uri()
I am working on a local WordPress install and I am trying to enqueue some stylesheets like I've done many times before but I've never run into this bug before.
Here is my code in the functions.php file.
function foundation_styles() {
wp_enqueue_style( 'foundation', get_template_directory() . '/css/foundation.css' );
}
add_action( 'wp_enqueue_scripts', 'foundation_styles' );
When viewing page source this is the file path that was enqueued:
<link rel='stylesheet' id='foundation-css' href='http://localhost/FoundationwebsiteC:xampphtdocsFoundationwebsite/wp-content/themes/foundation/css/foundation.css?ver=4.0' >
Instead it should be:
<link rel='stylesheet' id='foundation-css' href='http://localhost/Foundationwebsite/wp-content/themes/foundation/css/foundation.css?ver=4.0' >
Notice the C:xampphtdocsFoundationwebsite/ hidden in the middle of the file path.
The exact same thing happened when I used get_stylesheet_directory() instead of get_template_directory()
The filepath for my local install is:
C:\xampp\htdocs\Foundationwebsite\wp-content\themes\foundation\css
Anyone know what is causing my filepath to be so funky?
You're using functions that will return a path whereas what you need is a URL.
Replace get_template_directory() or get_stylesheet_directory() with get_template_directory_uri() or get_stylesheet_directory_uri().
Example:
function foundation_styles() {
wp_enqueue_style( 'foundation', get_template_directory_uri() . '/css/foundation.css' );
}
use this
function foundation_styles() {
wp_enqueue_style( 'foundation', get_bloginfo('template_url') . '/css/foundation.css' );
}
for more information refer this link
http://codex.wordpress.org/Function_Reference/wp_enqueue_style
I'm trying to get a url to my plugin, but plugins_url function returns an incorrect one.
in main plugin file there's this line:
$this->plugin_url = plugins_url( '/', __FILE__ );
Note, I've changed my plugins folder through wp-config.php:
...
define( 'WP_PLUGIN_DIR', '/home/victor/hg/' );
define( 'WP_PLUGIN_URL', 'http://hg.victorpc.org' );
...
hg.victorpc.org is a vhost with document root set to /home/victor/hg
the function returns this URL http://hg.victorpc.org/home/victor/hg/<plugin-folder> and the correct is http://hg.victorpc.org/hg/<plugin-folder>
Use plugin_dir_url() instead.
Codex: http://codex.wordpress.org/Function_Reference/plugin_dir_url
Usage:
$this->plugin_url = plugin_dir_url( __FILE__ );
This will return: http://hg.victorpc.org/hg/<plugin-folder>/ (notice the trailing slash).
I've worked around it, using:
plugins_url( basename( __DIR__ ) );
which returns what I expect.