How would I include this jquery code to wordpress? - php

I have been looking to figure this out for weeks. How to include a jquery in wordpress... so I have used the wp_enqeue_scripts();
but then I still don't understand it.
so for example I have this jquery slide down code..
$jQuery(document).ready(function() {
$jQuery('#slide').slideDown();
});
now this code works perfectly on my static html code which has the slide ID in it by the way..
so how would i include that slide to my wordpress plugin?
here is my plugin code
<?php
/*
Plugin Name: Jquery Test.
Plugin URI: http://wsplugins.com
Description: A plugin that increases your website traffic by floating facebook, twitter and google + share button. The buttons bounce and move over your page ensuring maximum attention by your visitors.
Author: Ronny Kibet.
Author URI: http://#.com
Version: 1.0
*/
thanks.

You have a couple of options.
Option #1
Paste the code into footer.php (or header.php).
<script>
$(function() {
$jQuery('#slide').slideDown();
});
</script>
This would result in every DOM element with an ID of "slide" to slideDown upon page load.
Option #2
Using WordPress' API, you could create a method in your theme to include a JavaScript file with the code inside . This is more complicated and requires an understanding of WordPress hooks.

I am having this same problem, but I got it to "somewhat" work. First you need to use wp_register_scripts, with the script name, and then do wp_enque_script("scriptname");... I don't understand why this is the way it is, but we just have to deal :-/

you may need to use noConflict, an example would be:
<script type="text/javascript">
var slide=jQuery.noConflict();
slide(function() {
slide('#slide').slideDown();
});
</script>

to enqueue a script in wordpress in a plugin use this:
/**
* Proper way to enqueue scripts
*/
function theme_name_scripts() {
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array('dependencyScriptThatNeedsToExecuteBeforeMyScriptName','forExample','jquery), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

I was also searching for the same solution but I found it myself.
create a new file custom-functions.js in the js director and past the code that you want to enqueue in that file. Now enqueue it in the functions.php
wp_enqueue_script( 'custom-functions', get_template_directory_uri() . '/js/custom-functions.js', array(), '1.0', true );
read the full tutorial at Bootstrap WordPress Snippets - wp_enqueue_script

function theme_name_scripts() {
wp_enqueue_script( 'script-name', plugins_url( '/js/responsiveslides.min.js' , __FILE__ ), array(),false, true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

Related

enqueue external plain JS from plugin php to add within footer of theme

I am trying to enqueue an external plain JavaScript file from within my plugin php, to add within my footer of the WP theme. Is this possible?
I've tried the below, but test is failing as no alert is firing...
<?php
/**
Plugin Name: Add Footer JS
*/
add_action( 'wp_footer', 'my_footer_scripts' );
function my_footer_scripts(){
?>
<script>alert( 'Hi Roy' ); </script> // just a test
<?php
}
So basically; the JS would be at:
http://dev.website.com/wp-content/plugins/mykewlplugin/js/script-support.js
And this PHP, at:
http://dev.website.com/wp-content/plugins/mykewlplugin/support.php
And core plugin PHP/functionality at:
http://dev.website.com/wp-content/plugins/mykewlplugin/mykewlplugin.php
Make sure that your currently active theme is making use of the wp_footer() and wp_head() functions, as those are required to utilize the appropriate action hooks. With that said, you should really properly enqueue your assets. Anybody else working on the project (including your future self) will be appreciative of that!
This involves utilizing the wp_enqueue_scripts hook and the wp_enqueue_script() function (which relies on both the wp_head() and wp_footer functions).
The proper way to do this would be like the following:
add_action( 'wp_enqueue_scripts', 'no_spex_footer_js' );
function no_spex_footer_js(){
wp_enqueue_script( 'no-spex-footer-js', '/path/to/your/file.js', array(), null, true );
}
Note all the passed arguments, in order:
a unique handle
the path to your actual file
an array of dependencies by handle (if you rely on jQuery, change this to array('jquery'), etc.
version of the file. I use filemtime( plugin_dir_path( __FILE__ ) . 'assets/file-name.js' typically.
in_footer - This is the important one, and determines if the file should be loaded into the header or footer.
If you're sure your theme is making use of wp_head() and wp_footer(), and the file isn't working - it means your plugin isn't configured/activated properly, at which point I'd recommend another cursory glance at the Writing A Plugin documentation and the File Header Requirements.
For your specific case, something like this should get you started:
mykewlplugin.php:
<?php
/**
* Plugin Name: My Kewl Plugin
* Description: Does Kewl Things
* Version: 0.1
*/
add_action( 'wp_enqueue_scripts', 'my_kewl_plugin_footer_js' );
function my_kewl_plugin_footer_js(){
wp_enqueue_script( 'my-kewl-plugin-footer', plugins_url( '/js/script-support.js', __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . 'js/script-support.js', true );
}
?>

Bootstrap CDN + WP // How to edit '.css’

I’m building my website on Wordpress + Bootstrap CDN.
I decided to make it on CDN to have no problem with updates in the future plus I read that this way it’s a bit faster.
So the thing is I have a problem with styles.
I imported my local ‘style.css’ to ‘header.php’, but since ‘bootstrap.min.css’ from CDN also has its own parameters I can’t apply some things.
How do I rewrite CDN’s parameters? Or is there a way to edit this exact ‘bootstrap.min.css’ file?
Thanks in advance.
You can simply just use wp_enqueue_style to load those files. Here is an example you can get an idea from.
function enqueue_my_scripts() {
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', array('jquery'), '1.9.1', true); // we need the jquery library for bootsrap js to function
wp_enqueue_script( 'bootstrap-js', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js', array('jquery'), true); // all the bootstrap javascript goodness
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
function enqueue_my_styles() {
wp_enqueue_style( 'bootstrap', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css' );
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_my_styles');
and to write the rules just use !important in your style.css file.
When you use wp_enqueue_style, you have the option to take control on where your styles and script be used for further references. Here is an example how to use it.
function my_enqueue_stuff() {
if ( is_page( 'landing-page-template-one' ) ) {
/** Call landing-page-template-one enqueue */
} else {
/** Call regular enqueue */
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
#TylerH answered my question and it solved my problem.
A better technique would be to include your own CSS file after you include the CDN file, and just write whichever styles you need to this CSS file of yours. That way they will override the problematic styles in your CDN file.
Thank you very much!

wordpress ondomready php mode

i have asked the question before, but i still can not figure out the full solution.
i want to make my wordpress site menu keyboard accessible, so far the dropdowns will show on tab but will not tab to the elements in the dropdown.
here is my fiddle: jsfiddle.net/X96gX/10/show/
it looks fine here, but when i tried to add the js to my wordpress functions.php im guessing it did not take in ondomready, but i have no idea how to customize that in php
any hints are appreciated!
edit: here is the code added to functions.php (it does show when i load the file as script in the header)
function includes_header_tab()
{
wp_enqueue_script( 'header-tab', dirname( get_bloginfo('stylesheet_url')).'/js/header-tab.js', array( 'jquery' ));
}
add_action('wp_enqueue_scripts', 'includes_header_tab');
function includes_header_jquery()
{
wp_enqueue_script( 'header-jquery', dirname( get_bloginfo('stylesheet_url')).'/js/header-jquery.js', array( 'jquery' ));
}
add_action('wp_enqueue_scripts', 'includes_header_jquery');
Inside your JS file, wrap the code within the DOMReady handler:
$(function() {
// existing JS code here.
});

Custom google map on contact page in wordpress

I need to put a styled map on a contact page in WP.
I'd rather not use a plugin as it would be overkill, embedding on the other way won't allow me to customize layers, use placeholders, etc
I coded an example map on a static html page. https://dl.dropboxusercontent.com/u/13823768/map/test.html
How do I get from here to wordpress?
EDIT: I'm working with a child theme so I put this in functions.php (in my child-theme dir)
function enqueue_custom_scripts() {
wp_enqueue_script('google-map-api','https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&language=it');
wp_enqueue_script('google-map-style', get_stylesheet_directory_uri() . '/js/map.js', array(), '1.0.0', true );
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
?>
Looks ok so far but it needs <body onload="initialize()"> too.
Do you know how I can add the onload to the body tag?
You can accomplish this by creating a custom theme page template: http://codex.wordpress.org/Page_Templates or simply add the #map_canvas element in the "text" view of the WSYWYG content area of the page editor.
The next step is to add all of your map scripts, I would do this by enqueuing the scripts in your theme's functions.php by creating a callback function that is called on the wp_enqueue_scripts action: http://codex.wordpress.org/Function_Reference/wp_enqueue_script.
the function you will add to functions.php would look something like this(replacing the filepaths with your scripts):
/**
* Proper way to enqueue scripts and styles
*/
function theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
I know you mentioned not wanting a plugin for overkill, but a plugin would allow portability of your code and would allow you to switch themes without losing your map code. Adding a plugin and shortcode to render the #map_canvas element would not be much more time than adding the code to functions.php. If your interested in writing a custom plugin, http://codex.wordpress.org/Writing_a_Plugin

Include new Javascript into wordpress

I recently started working on a wordpress site.
I have the woo commerce plugin working and he mystile theme. SO far so good!
However the gallery sucks!
I am trying to fiddle aroundwith it, but do not know my way around wordpress too well.
Question:
How do I add a line into the header?
Say for the single product, I would want to include a Javascript file. Do you know how I would do that?
What file do I augment?
Please let me know if you need more information.
Bo
To add a javascript file to the wordpress theme, you have 2 alternative:
Suppose we have a jquery.js inside the directory root of the theme.
Method A)
Open header.php file in wordpress theme. Add this inside <head> tag:
<script src="<?php echo get_template_directory_uri(); ?>/jquery.js"></script>
Method B)
Open functions.php file in wordpress theme:
Search something like add_action( 'wp_enqueue_scripts', 'blahblah' );,
If exist, find a function with blahblah name and add this code inside it:
wp_enqueue_script(
'jquery',
get_template_directory_uri() . '/jquery.js', array(), '1.8.3', false );
Otherwise: You should add a function and add_action method:
function scripts_styles() {
wp_enqueue_script(
'jquery',
get_template_directory_uri() . '/jquery.js', array(), '1.8.3', false );
}
add_action( 'wp_enqueue_scripts', 'scripts_styles' );
Note: Replace jquery string with your file name so.

Categories