How to set a variable on functions.php only if the viewed page is the homepage - wordpress

i am trying to run some PHP code on functions.php to clean some CSSs and add another ones. For now, for testing purpuse i am passing a parameter on the URL and if the parameter is present the code run else, the web load without the PHP code. I need to do a lot of tests on every URL of the site before leave the code running on all the web.
For now, the code is working only for homepage and I want to leave the code running, when i optimize another page i will to add it, etc etc, but I can't use the is_front_page() or is_page() function on functions.php, so what can I do?
I tried adding add_action but this is running after my PHP code, is there a way to use the is_front_page() function on functions.php or i need to change the logic of my code?
Note: I am a developer but I don't know enough about the Wordpress core and how to code in WP.
Note2: I read a lot of questions here and none of them help me.

I don't know your code, but maybe it's ok for you to write your code in a function in functions.php and use that function in page.php template?
How does your add_action code looks?
This should work too:
add_action('get_header', function() {
if ( is_front_page() ) {
// do it
}
});

Related

WordPress using different CSS - is this possible?

Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen

Wordpress shortcode does not work

I'm trying to create a simple shortcode function that would work in a link like this:
href="http://www.website.com">[short_code]
Please note, I had to remove the a tag because it wouldn't show. I want to be able to change the anchor text of a link with a single edit across multiple pages/posts.
to accomplish this, this is what I've done:
I've added include 'myshortcodes.php'; to functions.php file and created a file called myshortcodes.php
within that file I have the following code:
<?php
add_shortcode('hw', 'hello');
function hello() {
return 'Click Here For More Info';
}
Now when I put [hw] in href="http://www.website.com">[hw] it does work on my local wp install, however, when I do the same on my live server, it doesn't work, no matter what I do. It returns [hw] instead of 'Click Here For More Info'. Any ideas why? Thanks!
Maybe problems with php interpreter on server side.
Check if your server side doing include right.
Below and above your shortcode function place echo with something, and if it will echoes on your site, then we need to dig deeper.
And if it will not echoes, than the problem is in include function.

scripts not showing in custom wordpress theme

So, I've built my own theme for wordpress. Now, I'm trying to put my own plug-in on that theme which requires 3 javascript files. Upon reading the WP-Codex, it tells me the wp_register_script, wp_enqueue_script and add_action methods are the best way to go.
Now, in my plug-in file, I've done things such as:
function register_my_scripts() {
wp_register_script('script-1', plugins_url('path/to/script-1.js'), array('jquery'));
wp_register_script('script-2', plugins_url('path/to/script-2.js'));
wp_enqueue_script('script-1');
wp_enqueue_script('script-2');
}
add_action('wp_enqueue_scripts', 'register_my_scripts');
Nothing seems to show up on any of my template pages. I've even put this code on the main index page and still nothing. I've written something simple, straight from the codex like: wp_enqueue_script('jquery') on the page and still nothing shows up. Is there something I'm missing here? Why won't html for loading the scripts show up on my page?
Also, I'm running Wordpress 3.5.2
I enqueue my scripts like this on the functions.php file:
PHP
wp_enqueue_script ('customjs', '/wp-content/themes/blackboard/js/custom.js','','',true);
Please remember that the usage is:
<?php wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); ?>
Try putting the wp_enqueue_script() on your functions.php just as a test. Please check that your path is correct too and check the source code to see if it's printing but just with a wrong path.
Note that the first line of code on this answer is the only thing I need to enqueue the script, nothing else as far as I know.
Let us know if this works for you, cheers.

Calling wp_enqueue_media() in a custom theme widget on WordPress 3.5.x cause js error

I am writing a custom widget for my own WordPress theme.
From WordPress 3.5 there is a new Media Uploader instead of the old ThickBox.
My widget used to work fine on WordPress versions older than 3.5, but now the new media uploader prevent the old working behavior.
I added a check in the costructor for the presence of wp_enqueue_media function:
if( function_exists( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
but when this part of cose is executed javascript throw an error in the console stopping Js engine:
Uncaught TypeError: Cannot read property 'id' of undefined load-scripts.php:69
I removed all the widget code and reduced it to bare bones... the error is caused by wp_enqueue_media() calls, but I cannot get my head around why and how to fix it.
I also read Wordpress 3.5 custom media upload for your theme options, but there is no mention to this issue
Can anyone point me in the right direction? Is there any documentation available for the the WordPress 3.5 Media Uploader?
It's too late for you now, but might be helpful for other people. I managed to make it work using
add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );
Hope it helps!
The problem you are experiencing is because you probably put your custom jquery in the header and you didn't registered wordpress jquery. If multiple jquery are defined you will get that error.
My sugestion is you should either remove your jquery script or remove the one from wordpress
function remove_jquery() {
wp_deregister_script('jquery');
//wp_register_script('jquery', ("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"), false);
}
if(!is_admin()){add_action('init', 'remove_jquery');}
I suggest you use the jquery wordpress provides you, if not, the proper way to enqueue it is to deregister the default one an register your jquery. Just remove the comments from the remove_jquery function.
Also, the above code should go in functions.php
Cheers.
From codex [1], the function wp_enqueue_media( $args ) should be called from 'admin_equeue_scripts' action hook. or later.
Example:
function enqueue_media() {
if( function_exists( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
}
add_action('admin_enqueue_scripts', 'enqueue_media');
Hope it helped.
[1]. https://codex.wordpress.org/Function_Reference/wp_enqueue_media
To debug, you need to get the non-minified versions of the js sent to the browser. See the docs:
SCRIPT_DEBUG
SCRIPT_DEBUG is a related constant that will force WordPress to use the "dev" versions of core CSS and Javascript files rather than the minified versions that are normally loaded. This is useful when you are testing modifications to any built-in .js or .css files. Default is false.
define('SCRIPT_DEBUG', true);

Wordpress plugin functions: check if function exists

I'm developing a Wordpress site that relies on a plugin to be activated for the site to function properly.
The plugin has a few useful functions that I'm using in the site's template files. When the plugin is active, everything works perfectly. If the plugin is deactivated, the content doesn't load.
Wrapping these functions in if(function_exists(...) obviously fixes that, but I'm wondering if there's a cleaner way of doing that in Wordpress. Is there a function that can be placed in the theme's functions.php file that can check if these functions are available every time I call them, and if not provide a safe fallback without me having to wrap them in the function_exists()?
Thanks.
If you're only using it sparingly (1-2 times), use if( function_exists() ). If you're calling the function several times through in different template files, I'd suggest using something like
In your functions.php
function mytheme_related_posts( $someparams = nil ) {
if( function_exists( 'related_posts' ) ) {
related_posts( $someparams );
} else {
echo 'Please enable related posts plugin';
}
}
Then use mytheme_related_posts() in your template.
I think this is the most clear way. It prevents all problems. I think you can write a function instead which can check if these functions are available every time you call them, but I'm almost sure it can cause you more trouble and it burns more memory then simply using if(function_exist()). Don't forget the else branch and it will work fine.
If you want to check if a plugin is active then you should be using the is_plugin_active() function - you can find the docs at: http://codex.wordpress.org/Function_Reference/is_plugin_active
You can then also use if(function_exists()) as well just to doubly make sure :)

Resources