Not able to use a MailChimp popup - wordpress

Since this post got flagged as off-topic on WordPress StackExchange, I'll try my chances here.
My original post on StackExchange
Recently I tried integrating a mailchimp popup. The code is directly generated by mailchimp, I'm not editing it whatsoever.
I've tried implementing it several ways.
On the page via RAW JS.
Via a plugin called "Header & footer scripts".
Via a custom JS box in the Theme Options (this rendered a syntax error.)
via wp_enqueue_scripts in my child functions.php, which also generates an error in the console.
This is the script:
<script type="text/javascript"
src="//downloads.mailchimp.com/js/signup-forms/popup/embed.js" data-
dojo-config="usePlainJson: true, isDebug: false"></script><script
type="text/javascript">require(["mojo/signup-forms/Loader"],
function(L) { L.start({"baseUrl":"mc.us18.list-
manage.com","uuid":"d86301311141426b96c33360e","lid":"c46a6060b8"}) })</script>
I'm not really sure if I'm allowed to ask this question here, but I'm at my wit's end.
As far as I'm aware there is a syntax error in the code somewhere, but then again I wonder, why would MC push out code with an error.
I've already contacted MailChimp about this issue, they told me that the code looks fine and that there is nothing wrong with it.

There is nothing wrong with the code itself. The problem is when you copied and pasted you introduced line breaks which were not there. I ran the following and it works.
add_action( 'wp_head', function() {
?>
<script type="text/javascript"
src="//downloads.mailchimp.com/js/signup-forms/popup/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script><script
type="text/javascript">require(["mojo/signup-forms/Loader"],
function(L) { L.start({"baseUrl":"mc.us18.list-manage.com","uuid":"d86301311141426b96c33360e","lid":"c46a6060b8"}) })</script>
<?php
} );
Observe data-dojo-config and mc.us18.list-manage.com
Probably you have configured your editor to do auto word wrap on hyphens which will not work when editing code.

Related

Wordpress output using custom url

I've been using WP_Route to create some custom urls as:
domain.com/players/playerID
If I use wp_send_json with a query result it works fine, but I would like to print that result.
If I do an :
get_header();
echo "<h1>".$player->name."</h1>";
get_footer();
I get:
NAME
OOPS! THAT PAGE CAN’T BE FOUND
any clues?
How could I force WP to don't run content hooks/whatever ?
PS: Wp_die() adds another header and body tag after the "good content".
<body id="error-page">
<p></p>
</body>
If you are using Anthony Budd's WP_Route, then don't. It's not a finished product (it doesn't work). It's surprising that the author has pushed this class without even testing it. Other users have made pull requests, presumably fixing some of the bugs and glaring omissions, but it seems that the author isn't responding so consider the project dead. There are forks, however, that might be in working condition.
You should probably just exit; in your handler.
When you call the wp_die() function it typically outputs a full HTML document, including the <html>, <head> and <body> tags as well as some CSS and an error message, and this is probably not what you want in your handler.
As the WP_Route class is written, your handler is executed on the WordPress init hook.

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.

WordPress: hooks into mouse events?

I've spent a few hours on this but I'm not getting anywhere. I'm just starting on Wordpress, and I'd like a simple page where I click on something (a list item) and a textual description appears in a div somewhere else on the same page.
This would be trivial if I was writing plain HTML and JS, but I can't get my head around how to integrate this into WordPress. As far as I can make out, I have to write a plugin, but I can't find any handlers for mouse events in the hooks API. Or should I just hardcode an onclick into the HTML, and find somewhere to put some JavaScript code to handle it? Any advice appreciated...
Wordpress doesn't have mouse events/hooks. Wordpress hooks apply to the backend only, they are a way to interact with the WP core which is written in PHP (executed on the server, not the client).
Mouse events happen on the client side, so to achieve what you want you should register a Javascript file with wordpress via wp_register_script (http://codex.wordpress.org/Function_Reference/wp_register_script) and add your Javascript behaviour there.
You don't have to write a plugin, just add wp_register_script logic in your functions.php.
Vlad Cazacu is right, we dont have any hooks for javascript events and i am not sure if there is any option in other server side languages as well besides node.js. Anyways you can use jquery in the normal way with registering and enqueing the file. But if you want to have advanced interactivity then there are functions in wordpress that can do that for instance there's this function wp_localize_script, it is used with ajax to grabs the data as a javascript array/object and then converts it into php array/object which is then available to use in wordpress/php.
In short -
Add an id (and a styling class) to whatever you want a handler for
in the HTML
Register handlers in JS, as follows:
function fp_onload_js() {
var id = document.getElementById('myID');
id.addEventListener(
"click",
function() { myEventListener(); },
false);
}
Register/enqueue the file which contains your JS, in functions.php:
add_action('wp_enqueue_scripts', 'theme_enqueue_stuff');
function theme_enqueue_stuff() {
...
wp_enqueue_script(
'myHandle',
get_template_directory_uri() . '/path_to_my_js_file.js');
}
The tricky bit: you have to make sure that the event listener code is run after WordPress has constructed the DOM, after the IDs have been created. You need to run the JS after the page loads (see here). Basically, also in functions.php:
add_action('wp_footer', 'fp_onload_php');
function fp_onload_php() {
?>
<script type="text/javascript">
fp_onload_js();
</script>
<?php
}
And make sure you don't use the 'visual' tab in the WordPress editor - it will mess up your carefully-crafted HTML.

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.

Bullet proof way to avoid jquery conflicts on wordpress plugins

I have been developing wordpress plugins for a while now and i always seem to get the following issues with all my plugins Jquery conflicts issues.
I have tried so many different ways to avoid these but i always get users contacting me saying when they have installed one off my plugins it has stopped another plugin from working aahhhhh.
I really want to get this sorted because i understand how frustrating this can be for people.
I always set and option or include wordpresses jquery, below is just an example not working code.
add_action( 'init', array( $this, 'include_jquery' ) );
function include_jquery(){
wp_deregister_script('jquery');
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"), false, '1.7.1');
wp_enqueue_script('jquery');
}
Ok so after issues with this i now have a select option in the plugin admin to toggle yes or no to include jquery or not i know it is automatically installed but some users remove this, this works for some people but not all.
if you include the wordpress jquery i know you have to run your jquery with the following.
jQuery(document).ready(function ($) {
jQuery instead of the dollar sign $
i understand and have used jquery no conflict and tried and tested some if not all off these
http://api.jquery.com/jQuery.noConflict/
$.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
});
This as with the others works for some but not all users with conflicts arising still with certain users.
I am hoping that from this post some of us wordpress plugin developers could help out and post a bullet proof way to use wordpress and jquery within our plugins without getting conflict issues.
Thanks
Doesn't it work with a closure?
(function($){
// your plugin code
})(jQuery);
Read these parts of the codex :
Load a default WordPress script from a non-default location
jQuery noConflict wrappers
You should use wp_enqueue_scripts hook instead of init.
And you should use jQuery.noConflict(); instead of $.noConflict();.

Resources