I am using jQuery load() to load a PHP page at runtime. And I am not using an elegant way to get the PHP URL. Somebody tell me an elegant way?
My method:
'http://' + document.domain + '/wp-content/themes/mytheme/ajax/myload.php'
If my wp is under a sub dir,I have to change the url to this:
'http://' + document.domain + '/mysubdir/wp-content/themes/mytheme/ajax/myload.php'
this is so ugly!
Structure of code is;
index.php contains a.js
a.js uses ajax to load tpl.php (just echo some div) into index.php
tpl.php is in my theme path.
So the question is how to get the URL of tpl.php in a.js?
You should be able to use get_template_directory_uri.
http://codex.wordpress.org/Function_Reference/get_template_directory_uri
Also, get_site_url may come in handy as well.
http://codex.wordpress.org/Function_Reference/get_site_url
And get_bloginfo gives you a bunch of stuff, like the site url and home directory url.
http://codex.wordpress.org/Function_Reference/get_bloginfo
Also, It sounds like you need to add a parameter to your JS code, which is "passed" from PHP.
For example (in pseudocode)
a.js:
loadDivFromThemePage(var themePagePath)
{
// call ajax load with themePagePath variable
}
index.php:
<script>
loadDivFromThemePage(<?php echo get_template_directory_uri() . "/tpl.php" ?>)
</script>
You need to use wp_localize_script.
https://codex.wordpress.org/Function_Reference/wp_localize_script
For example, in my javascript I call the variable 'myLocalized' when I need my path. I use Angular with WordPress. My code is below:
function() mes_scripts{
wp_register_script(
'angularjs',
get_stylesheet_directory_uri() . '/bower_components/angular/angular.min.js'
);
wp_register_script(
'angularjs-route',
get_stylesheet_directory_uri() . '/bower_components/angular-route/angular-route.min.js'
);
wp_enqueue_script(
'my-scripts',
get_stylesheet_directory_uri() . '/js/scripts.min.js',
array('angularjs', 'angularjs-route' )
);
wp_localize_script(
'my-scripts',
'myLocalized',
array(
'partials' => trailingslashit( get_template_dir
)
);
}
add_action( 'wp_enqueue_scripts', 'mes_scripts' );
Related
in the index.php of a custom plugin i have put this :
wp_enqueue_style( 'style_overview', get_stylesheet_directory_uri() . 'http://127.0.0.1/wp-content/plugins/profile_plugin/css/style_overview.css',false,'1.1','all');
But it returns this in the final head :
href="http://127.0.0.1/wp-content/themes/alchemistshttp://127.0.0.1/wp-content/plugins/profile_plugin/css/style_overview.css?ver=1.1"
Thanks all.
You are defining this path in the enqueue style function. With the dot . you are connecting with the following string.
You can use this to get the current plugins url:
plugin_dir_url( __FILE__ ) . '/css/style_overview.css'
Or you can just define it hard coded as you did:
'style_overview', 'http://127.0.0.1/wp-content/plugins/profile_plugin/css/style_overview.css'
The function you are using gives you the url where the style.css of your active theme is located:
get_stylesheet_directory_uri()
That is why you are getting your theme path followed by the url you added as a string.
Use plugins_url() and then append your directory or file structure on the end of that.
https://developer.wordpress.org/reference/functions/plugins_url/
Example:
wp_register_style(
'my-css',
plugins_url( '/blocks/dist/style.css', __FILE__ ),
array( 'wp-blocks' )
);
I am setting up a new Wordpress plugin with a custom metabox. This box contains sets of fields that I store in an array. I want the user to be able to add a new set of fields by clicking a button on the admin-page.
I read different posts on how to accomplish this but I havent succeeded yet. For testing purposes I created a real simple setup in the metabox to change a text of a element. This is also not working so I think it is a problem with loading the scripts correctly.
So what I did so far:
- add action through admin_enqueue_scripts
- register the script using wp_register_script
- enqueue script using wp_enqueue_script
- setup the js file (choose for testpurpose to store it in same dir as the plugin
function amai_woordjes_scripts() {
wp_register_script( 'amai_woordjes_updaten', 'amai_woordjes_updaten.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'amai_woordjes_updaten' );
}
add_action( 'admin_enqueue_scripts', 'amai_woordjes_scripts' );
//HTML code I use in the function which is called by add_meta_box
echo '<p id="demo">Current text</p>';
echo '<button id="woordje_toevoegen" onclick="woordjesToevoegen();">Woorden toevoegen</button>';
//amai_woordjes_updaten.js
<script>
function woordjesToevoegen() {
document.getElementById("demo").innerHTML = "New texxt";
}
</script>
You need use function wp_enque_script(). You use incorrect script path.
plugin_dir_path( __DIR__ ) . path/to/script.js
First You have to set the file path
Create js folder in your plugin root directory and move the 'amai_woordjes_updaten.js' file in js folder
function amai_woordjes_scripts() {
wp_register_script( 'amai_woordjes_updaten', plugin_dir_url( __FILE__ ).'js/amai_woordjes_updaten.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'amai_woordjes_updaten' );
}
add_action( 'admin_enqueue_scripts', 'amai_woordjes_scripts' );
I'm told it's very important to register and enqueue styles and scripts in WordPress even though it's a real pain in the behind. I had no problem with the scripts however when I try the styles, WordPress shows nothing in the code for stylesheets. I'd like to get in the habit of building my themes the correct way so I'd like to learn how this is done, but I can't understand why my css is not getting added to the code when the scripts are added. See code to functions file below:
<?php
function alpha_scripts(){
$script = get_template_directory_uri() . '/assets/scripts/';
wp_register_script('bootstrap-js', $script . 'bootstrap.min.js', array('jquery'),'', false);
//wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-js');
}
function alpha_styles(){
$style = get_template_directory_uri() . '/assets/styles/';
wp_register_style('bootstrap-css', $style . 'bootstrap.min.css','','', 'screen');
wp_register_style('alpha-css', $style . 'alpha.css','','', 'screen');
wp_enqueue_style('bootstrap-css');
wp_enqueue_style('alpha-css');
}
function alpha_menus() {
register_nav_menus(
array(
'main-menu' => __( 'Primary Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
add_action('wp_enqueue_scripts', 'alpha_scripts');
add_action('wp_enqueue_styles', 'alpha_styles');
add_action('after_setup_theme', 'alpha_menus');
?>
What am I doing wrong here? I've tried using different references for the url, like get_template_directory_uri() and get_stylesheet_directory_uri() but as I suspected those two made no difference.
It looks like your registering and enqueueing correctly; but I suspect that the function itself is not being called because the action you are attaching it to isn't one defined by WordPress.
add_action('wp_enqueue_styles', 'alpha_styles');
should be:
add_action('wp_enqueue_scripts', 'alpha_styles');
wp_enqueue_scripts is the hook that should be used for both scripts and styles. (See: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts)
If your unsure about a hook in the future, you can check if it exists in the action reference here: https://codex.wordpress.org/Plugin_API/Action_Reference
I have read that the correct way to Load your style sheet in WordPress is to use wp_enqueue_style in functions.php.
If i have different CSS for my index and home and other pages, how will the Server/WordPress know which style sheet is to be linked with a particular page and send to browser, or will it send all the CSS files to browser?
In your theme's functions.php file you can add code similar to this, please note that this example assumes the new CSS files live in the css/ directory within your theme folder. If you don't have your folder structure like that, that's okay, just modify the path of the CSS file locations within the wp_enqueue_style() call.
add_action( 'wp_print_styles', 'change_page_styles' );
function change_page_styles() {
// For the front page
if ( is_front_page() ) {
wp_enqueue_style( 'front', get_stylesheet_directory_uri() . '/css/front.css' );
}
// For the blog page
if ( is_home() ) {
wp_enqueue_style( 'blog', get_stylesheet_directory_uri() . '/css/blog.css' );
}
// For regular pages
if (is_page() && !is_front_page()) {
wp_enqueue_style( 'pages', get_stylesheet_directory_uri() . '/css/pages.css' );
}
}
If you need other conditional tags to call out specific pages, post types, categories, etc. you can find the appropriate tag here: https://codex.wordpress.org/Conditional_Tags
I am having an issue with loading some javascript in a child theme I am creating for one of my themes.
Essentially I do:
get_stylesheet_directory() . '/path/to/javascript.js'
which is what your suppose to do, But , as per the documentation, I am getting:
mysite.com/dev/home/path/to/wordpress/path/to/theme/path/to/js.js
which is causing it to not find the required file. because home is at the root of the site.
any ideas on what I can do to fix this?
get_stylesheet_directory() returns a server path. I'm pretty sure you want:
get_bloginfo('stylesheet_directory');
That will return a URL of your stylesheet directory in relation to your Wordpress Installation so you can load external assets like Javascript files that reside in your theme.
I think this will do the trick:
<script type="text/javascript" src="<?php echo get_stylesheet_directory_uri();?>/path/to/javascript.js"></script>
It's better to enqueue scripts and styles the WordPress way.
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
https://developer.wordpress.org/reference/functions/wp_enqueue_style/
Put this in functions.php
add_action( 'wp_enqueue_scripts', 'enqueue' );
function enqueue() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_style( 'theme-style', get_stylesheet_directory_uri().'/css/theme.css' );
wp_enqueue_script( 'theme-scripts', get_stylesheet_directory_uri().'/js/theme.js' );
}