Wordpress output using custom url - wordpress

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.

Related

Not able to use a MailChimp popup

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.

WordPress Template "X": Change html inside of <head>

I'm pretty new to WordPress and want to remove some unneeded js/css files that are loaded inside of the html-head of the page.
In wp-content/themes/x/framework/views/header/base.php I found the following code:
<head>
<?php wp_head(); ?>
</head>
This obviously doesn't help me at all. I have no idea what WordPress' "wp_head()"-function does next. Isn't there some simple file where I can just write/edit simple HTML for the "head" somewhere?
wp_head() is a function that fires the wp_head action hook. Wordpress is built on action hooks and filters, which means that not only can plugins and themes add things to your setup through these hooks, but you have the ability to remove things as well. Most of this is done programmatically (in PHP) instead of there just being an html template, although that's not always the case... but it IS the case in your case.
Scripts and styles, for the most part, are enqueued through the wp_enqueue_scripts action hook. You'll want to perform your script removals here.
The problem is, you'll have to know the name the script was registered as before you can remove it. For instance, jQuery is registered as 'jquery'. Pretty simple, right? However, not all scripts are going to be that simple, and you'll have to browse through the code to find the registered handles (names) for those scripts. Styles are a bit easier, as when you inspect your page in the browser, styles will have an id attribute set to 'example-style-css'. Wordpress appends the '-css' to registered and enqueued styles, so the name of your style is actually 'example-style'.
In your theme's functions.php, you can dequeue scripts and styles so they won't be included on your page like so:
function stack_46669800_dequeue(){
wp_dequeue_script('the-script-you-want-to-remove');
wp_dequeue_style('the-style-you-want-to-remove');
}
add_action('wp_enqueue_scripts','stack_46669800_dequeue',100);
(Notice the 100 in the add_action function here... This is the 'priority', or the order in which all added actions are fired. Most are at 10, so I changed the priority here to 100 so this would be fired presumably later than whatever is enqueueing your unwanted scripts. You can't dequeue something that hasn't been enqueued yet.)

Wordpress (Pods) oEmbed Items

I'm using the fantastic Pods plugin to extend Wordpress's basic content types with a few custom ones. I've build an advanced custom type which means I don't get the automatic oEmbed support built into the native page/post types. I've structured it so my custom content type has a pod page using a PHP page template and I have the oEmbed option enabled for my WYISWYG fields that can embed videos.
I found this post which seems to indicate that a basic apply_filter function should automatically handle any embeds but I can't seem to get it to work. I'm a bit new to filters. The code I tried is below:
<?php
// Fetch body field content from $pods object
$mycontent = $pods->field('field_body');
$output = apply_filters('oembed_dataparse', $mycontent);
echo $output;
?>
I tried a variety of different filters such as the_content and others but none seemed to work. I believe it may be a scoping/conflict issue with Pod pages since even writing out entire iFrame embed code into the template won't work but only displays an empty iFrame. The global oembed function does the same, i.e.
$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
In the context of the page template will output:
<iframe width="500" height="375" frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/dQw4w9WgXcQ?feature=oembed">
<html>
<head>
</head>
<body>
</body>
</html>
</iframe>
field() gets the value of the field, display() gets the output of the field (ran through any related filters / functions the field is configured to run through).
$mycontent = $pods->field('field_body');
should be
$mycontent = $pods->display('field_body');
For more information, see http://pods.io/docs/field/ vs http://pods.io/docs/display/
Calling apply_filters('oembed_dataparse', $mycontent) is incorrect since this meant to add functionality for processing other data types (photo, video etc) not catered for by default. What you want to do is mimic how WordPress does the embedding. I haven't tested the code below, but it seems to me the way to go about triggering the embed functionality:
global $wp_embed;
$mycontent = $pods->field('field_body');
$output = $wp_embed->autoembed($mycontent);
echo $output;

To get the value in header section

I started to develop a plugin and i write the code bellow
add_filter('get_header', 'my_postslide');
It is calling the function in body part, but now i want to get the value in the header part.
It means in the <header> </header> part.Please let me know what the appropriate hook for call the value in the "<header> </header>" part.
Use the wp_headhook. see here in WordPress codex : http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

How do I load jQuery for only one specific page in my WordPress theme?

I'm fiddling with a WordPress theme. I'm aware I can use wp_enqueue_script in my header.php to load WordPress's supplied jQuery library.
I was just going to use wp_enqueue_script in my header, but it seems inefficient when I only want to use it on a particular Page (just on one single page with a particular page_id.)
Given that, what's the best way of including jQuery only on one particular Page?
Presumably I can't do page_id detection in header.php, because that won't be in The Loop, right? But I'm guessing I'm missing some fairly obvious method -- I'm fairly new to theme development...
Thanks!
Yes you can, is_page doesn't need to be called in The Loop, since it doesn't change when The Loop runs. So is_page(42) will only return TRUE if you're on the page with id 42. It also works with the page title or name (slug), which might be more future-proof if you ever replace delete this page and replace it with a new one with the same slug.
here is an article about dynamic body ids
http://perishablepress.com/press/2009/05/26/dynamic-body-class-id-php-wordpress/
after you get your page name you can add a conditional statement in your template index.php that says something like this in your page header or before the closing body tag:
// $page_name would be the page name you extracted with a function from the post
if($page_name === 'about'){
echo '<script type="text/javascript" src="jquery.js"></script>'
}

Resources