Adding a script to two pages in my site - php

Im trying to get my Google Maps js loaded on two pages. One is a page template and the other is a single post type page.
template-homepage.php
single-destinations.php
I have a enqueue scripts file to load all my scripts in the header and footer. Here is the code im trying to use but its not loading the scripts at all.
if (is_singular('destinations') && is_page_template('template-homepage.php')) :
wp_enqueue_script( 'google-maps-js', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyebtdsdfgdAmyvSWk8wXmvdmtD83PR4vrCZuYs&libraries=places' );
endif;

Dan,
How can it be destinations AND template-homepage at the same time?
Try changing this:
if (is_singular('destinations') && is_page_template('template-homepage.php')) :
To:
if (is_singular('destinations') || is_page_template('template-homepage.php')) :

Related

Wordpress add script to footer for specific category (archive and posts)

i am trying to add a google ad script for specific category only. The code should be and in the archives page for that category and the posts of that category.
Here is the code I am using in my functions.php file
add_action('wp_footer','MY_FUNCTION');
function MY_FUNCTION(){
if ( is_category(309) ){
echo '<script>the script</script>';
}
?>
but for some reason it breaks the site
Use include instead of echo. Store the Script in a separate php file and the call that file via include.
it won't break the site.
For examble,
create a script.php file and add the script. The call the file via include.
add_action('wp_footer','MY_FUNCTION');
function MY_FUNCTION(){
if ( is_category(309) ){
include 'script.php';}
You can add "SOGO Add Script" plugin to use google ads script. It allows you to use javascript in header or footer section directly into your specified page.

Include PHP files depending on Wordpress Templates being used

I am using WordPress for my site. The site uses many different PHP page templates to display information to the user. At the moment I include all PHP files necessary for posting data to the backend of the site in a common functions.php file e.g. require_once("function1_functions.php"); require_once("functions2_functions.php"); etc
The issue I am having is all of my functions#_functions.php files are loaded on every page, I don't want this. There is an IF statement you can use in WordPress that will allow you to put call certain files on a page depending on the template (page) the user is currently on but this doesn't work for PHP files.
if (is_page_template( array('pagetemplate-about-us.php') )) {
// call JS files here that will only show on the "About us" page
require_once("functions_about_us.php"); <-- This doesn't work.
}
Any ideas?
The Conditional Tags which control what content is displayed with conditions (such as is_page_template(). Note these only work after the posts_selection action. So here the setup function is actioned at wp action, just after posts_selection.
More info here on Conditional Tags.
if (!function_exists('theme_php_include_setup')):
function theme_php_include_setup() {
if (is_page_template( array('pagetemplate-about-us.php') )) {
require_once("functions_about_us.php");
}
}
// apply conditions at appropriate WP action
add_action('wp', 'theme_php_include_setup');

Wordpress plugin - how to disable js/css per page

With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.
You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script

wordpress: Conditionally run javascript and load image for post pages

I have a javascript curl effect in place which loads an image and javascript file for the curl effect. But I want to appear in the post pages only.
So I want to do:
if this is a postpage{
load image;
load javascript;}
What is the way to do this in wordpress with php?
If you want to add it only to posts pages and homepages and your plug ins are troubling you..you might want to try
<?php if (is_single()|| is_home() || is_page()){ ?>
load image;
load js;
<?php } ?>
if you want it for only posts and pages, try
<?php if (is_single()|| is_page()){ ?>
load image;
load js;
<?php } ?>
not entierly sure what your after,
but you say you have the code ont he index.php page,
again un-sure has to, how you have this setup,
but if its only to be shown on a post or page or a postpage? (single post),
would be better to move your code into the header.php file? or have it only in the single.php file which means its only loaded when viewing a single post,
in turn it wont be shown on any other pages, except the single (full post) page..
then using the likes
if( is_single() ){
//then show - run your code?
}
?
Marty
Edit:
for both posts & pages then use
if( is_single() || is_page() ){
//then show - run your code?
}
this will only show your code when its a single post OR within a page.

Wordpress calling an HTML file from PHP

I am trying to modify a theme on wordpress. The theme shows a slider on the front page, which reads from the featured.php file.
I have gone into the featured.php and removed all the PHP code. I can inject my custom HTML into the featured.php and the pages display properly, however I want the page to display from a wordpress page.
So i tried to:
<?php
$html = file_get_contents('http://www.mydomain.com/homepage-featured');
?>
The above link corresponds to a page i created in wordpress. So i want to be able to inject my HTML into that page and then when you load my homepage, the PHP tells the browser to display the contents of this URL.
The above code doesn't work.
Thanks for the help.
file_get_contents() - as its name suggests - reads a file in but does not print it. You need to echo your $html variable. A better way is to use require() or include() but if I were you I would put my custom file on the same server so that way you don't have to use the file from a remote location thus sparing network traffic.
I think you have better to use the include function.
The file_get_contents you are using would generate an HTTP request, so it would make your script slower. I think it would be a good idea to put the HTML file on the same server if possible.
Have you tried
<?php
require('http://www.mydomain.com/homepage-featured');
?>
If my understanding is correct, you are trying to use a template (featured.php) for a different page other than the front page.
To do so, just change the Page Template of the page(the separate page which is # www.url.com/myhomepage). You can change this # Dashboard > Pages (Click Edit link in the required page) > Edit Page > Page Attributes meta box (available in the rightside below Publish) > Template. Change the template of this page to "Featured".
(I assume that your code in file feature.php has Template Name: Featured at the top)

Categories