is_home() || is_front_page() not working - wordpress

I have already tried everything, and searched in every place. Why doesn't it work?
This is my functions.php, the page always show "else".
My code:
wp_reset_query();
if(is_home() || is_front_page())
die("if");
die("else");

you can use a hook in your function.php to execute a piece of code before everything else like this:
add_action('wp', 'check_home');
function check_home() {
if(is_home() || is_front_page()) {
die("yes");
} else {
die("no");
}
}
You should never just write php statements that stand on it's own in your functions.php file. Only use it to define functions that your want to use somewhere else in your code, or define hooks like actions and filters with corresponding functions.
If you want to know more about hooks read this

Related

Calling a plugin's function from functions.php

I am writing some code in the functions.php of my theme, now what I need to do is execute a function from inside another plugin and im wondering if its possible.
The plugin is wooevents pro and the function I need is as follows:
function order_contains_tickets( $order ) {
// Function code is in here
}
So my code will look something like this
if ( order_contains_tickets( $order ) ) {
// Execute code
}
Technically, the exact code you have would work, but it's also unsafe. In the event that the wooevents pro plugin were ever disabled, the code you added would be referencing a function that no longer exists. Thus, a better option would be to add a check to ensure that the function exists like so...
if( function_exists( 'order_contains_tickets' ) ) {
if( order_contains_tickets( $order ) ){
// Execute code
}
}
It should also be noted that this is dependent on the load order of the two functions. The wooevents pro function must be loaded prior to your function for it to work.

How can I use is_page() inside a plugin?

I want my plugin to register a script only in a certain page.
For example, inside my plugin file I want to write something like this:
if (is_page()) {
$pageid_current = get_the_ID();
$page_slug = get_post($pageid_current)->post_name;
if ($page_slug == 'articles'){
wp_register_script('myscript', '/someurl/main.js');
}
}
But I get the error:
is_page was called incorrectly. Conditional query tags do not work
before the query is run. Before then, they always return false. Please
see Debugging in WordPress for more information. (This message was
added in version 3.1.)
How can I, inside of a plugin, register a script in a certain page?
is_page() only work within template files.
And to use it within plugin files, you need to use it with the combination of template_redirect action hook.
This action hook executes just before WordPress determines which template page to load.
So following snippet would work:
add_action( 'template_redirect', 'plugin_is_page' );
function plugin_is_page() {
if ( is_page( 'articles' ) ) {
wp_register_script( 'my-js-handler', '/someurl/main.js', [], '1.0.0', true );
}
}
You could use is_page() after template redirect so you need to add in the hook like this :
add_action('template_redirect','your_function');
function your_function(){
if ( is_page('test') ) {
// do you thing.
}
}
You must register your script as if you want it to work everywhere.
You can de-register it after the job is done, like this:
function deregister_my_script() {
if (!is_page('page-d-exemple') ) {
wp_deregister_script( 'custom-script-1' );
}
}
add_action('wp_print_scripts', 'deregister_my_script', 100 );

trouble adding jQuery UI to Wordpress admin page

I'm attempting to add a datepicker to a custom meta box in wordpress. If I enqueue the script like so
// add stylesheets for date picker
add_action('admin_print_styles', 'add_datepicker_styles');
function add_datepicker_styles() {
global $post;
$styleFile = get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
if(file_exists($styleFile) && $post->post_type == 'show') {
wp_enqueue_style("datepicker-css", $styleFile);
}
}
// add javascript for date picker
add_action('admin_print_scripts', 'add_datepicker_js');
function add_datepicker_js() {
global $post;
$jsFile = get_bloginfo("template_url").'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js';
if(file_exists($jsFile) && $post->post_type == 'show') {
wp_enqueue_script("datepicker-js", $jsFile);
}
}
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
global $post;
if($post->post_type == 'show') {
echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
}
}
I get an error that jQuery('#show_date').datepicker(); is not a function. When I check the processed source code I see that jQuery is loaded by default but the style and jQuery UI script are not loaded at all. The code attached to the admin_head hook loads fine and if I echo the script and styles in this function it works the way I want it to.
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
global $post;
if($post->post_type == 'show') {
echo "<link rel='stylesheet' type='text/css' href='".get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css'."' />";
echo "<script type='text/javascript' src='".get_bloginfo('template_url').'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js'."'></script>";
echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
}
}
So is it a problem with the wp_enqueue_style/script() functions or with the add_action hooks? I'm new to wordpress so I'm not really sure how to troubleshoot this. I've done a google search and everywhere I go the code looks like mine. Also if you are wondering the paths to the files are correct.
Can anyone think of a solution to my problem? Is it wrong to just echo the script and styles or should I enqueue them?
Thanks!
EDIT:
file_exists($jsFile) returns false. if I remove the conditional check for the file the code works.. But why? how else can you check for a file that exists using a url beginning with http:// as opposed to a local file path?
If you want to get a local path for your theme, you can use get_theme_root():
$styleFile = get_theme_root().'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
if(file_exists($styleFile) && $post->post_type == 'show') {
From php.net/file_exists:
this code here is in case you want to check if a file exists in
another server:
<?php function fileExists($path){
return (#fopen($path,"r")==true); } ?>
unfortunately the file_exists can't reach remote servers, so I used
the fopen function.

Custom post type functions.php if statement on action

I am using developing a child theme for Woothemes' Canvas.
I am trying to use functions.php in the child theme to only use actions on my custom post type.
This code doesn't seem to be working:
add_action( 'woo_post_inside_after', 'my_geo_mashup' );
function my_geo_mashup() {
echo GeoMashup::map();
if ($post->post_type == 'listings') {
//My function
}
}
add_action( 'woo_post_inside_before', 'listings_nivo' );
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
if ($post->post_type == 'listings') {
//My function
}
}
So, I'm unsure how to get the above to work properly and only show these items on the custom post type, or only for the custom post type template single-listings.php (as I only want the map and slider to show on the actual post, not on the blog page (archive.php)
Rather than making the entire $post object global, you can just make $post_type global instead. Ex below.
I'm not exactly sure where that function is being loaded, but make sure you hook somewhere within the post. If the action is before, as far as I know and from experience, the post variable will be null.
Just as a test, try running the action in wp_footer Ex. add_action( 'wp_footer', 'listings_nivo' );
See if that yeilds any results.
if echoing var_dump($post) is still null, well, not sure where to go from there.
So you can try running the below, then run the action in the appropriate place if it works:
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
global $post_type;
// Diagnostic purposes
echo var_dump($post_type);
if ($post_type == 'listings') {
//My function
}
}
add_action( 'wp_footer', 'listings_nivo' );
Check your error log or turn wp_debug to true in your wp-config.php file if nothing else to see if anything else is going on.
Best of luck!
Inside your function, try adding global $post;. Then to see what you are getting with $post->post_type echo it out to the screen. As long as this gives you "listings", your code should work. If not, there's probably another issue at play.

Run code block in functions.php ONLY when theme is activated. Use register_activation_hook?

I have a specific bit of setup code for my theme that I only want to process when the theme is first activated. I was under the impression that I could use register_activation_hook for this, but it does not appear to be working.
Example...
In my functions.php file, I want the doThemeSetup() function to run only upon theme activation and no other time...
function doThemeSetup(){
//stuff here only runs once, when theme is activated
}
register_activation_hook(__FILE__, 'doThemeSetup');
UPDATE: Since posting this question, I have found that register_activation_hook is only available to plugins and not themes.
I have found a means to do similar action in themes but I'm getting inconsistent results:
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' )
{
//do something
}
The code above does not seem to run when the theme is first activated, but rather when the theme is switched to from another theme.
This is more of a workaround than solution, but if everything other fails you can try it:
<?php
if (get_option('themename_installed') != 'true'){
if (doThemeSetup()){
add_option('themename_installed','true');
}
}
?>
This way the doThemeSetup is ran only once.
/**
* Install script to create databasetables and then insert default data.
* And inserting defautl theame settings.
* Only run if theme is being activated for the first time.
*/
$flag = get_option('first_time_theme_activation_check');
if ( $flag == false && is_admin())
{
// put your code to run when theme is activated at first time by admin
// update option at last
update_option('first_time_theme_activation_check', 'true');
}

Resources