Im currently trying to code my own shortcode to use the YouTube API on my WordPress website. My Code works also but only with the default attributes of the shortcode. The appended attributes via shortcode self are ignored by my code.
Can anyone help me? I googled a lot but I didnt found anything like this. I think I made everything like the codex way but maybe you know more.
Here is my code:
function api_youtube_integration($atts, $content = null) {
$a = shortcode_atts(array(
'class' => '',
'videoCount' => 5,
'bottomBar' => true
), $atts);
ob_start();
?>
<div data-video="<?php echo esc_html($a['videoCount']); ?>">Test</div>
<?php
return ob_get_clean();
}
add_shortcode('youtubeAPI', 'api_youtube_integration');
My shortcode: [youtubeAPI videoCount=12]
The code only returns 5 as videCount but not the 12...
Best Regards Lukas
So apparently attributes passed to shortcode_atts() are always converted to lowercase. Official docs:
Attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched. [myshortcode FOO="BAR"] produces $atts = array( 'foo' => 'BAR' ).
So you'll either need to make sure all options are lowercase, or use underscores to separate words.
Related
The goal is to have wordpress spit out or echo all of the images from a custom post format I've created. I'm new to PHP and I am not looking to use any 3rd party plugins, maybe a fix to the code provided below please.
All of the solutions I've came across from here (stackoverflow) or from google does not work or spits out ALL of the images I've ever uploaded from the media library, which I do not want. Most solutions provided are for "display the first image" which I know of and works, but I would like all of the images from a post.
Here's the closest code that sort of worked a couple of times but it deforms my layout, then goes back to displaying one image:
function displayPostImg() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[0][0];
return $first_img;
}
And the call I use to paste into my custom post php:
<?php echo displayPostImg(); ?>
As stated above, existing solutions do not work. Any help with a clear explination of what I am doing wrong will be a massive help.
EDIT
#corvidism Thanks for the further explanations man. I did have a look in the dev tools and the "return implode" seems to be taking it OUT of the loop, causing the layout issue. Your solution does show the post images/attachments I am after but I can't seem to fix this unfortuntaly. There seems to be this go to solution that's meant to do the same thing. This sadly echoes every image from the media library. Combining your code with this one below has gotten me close but if I come across a solution, I'll post it here for others:
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' =>'any',
'post_parent' => $post->ID );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
EDIT #2: Solution
After messing around with various solutions, I found something that works. Try it out:
function grab_those_images( $post ) {
$content_post = $post->post_content;
$search_post = '~src="[^"]*"~';
preg_match_all( $search_post, $content_post, $imgs );
$no_of_pics = count($imgs[0]);
if ( $no_of_pics > 0 ) {
for ( $i=0; $i < $no_of_pics ; $i++ ) {
$string=$imgs[0][$i];
$string=trim( $string );
$length=strlen( $string );
$image_path=substr_replace( substr( $string, 5, $length ),"",-1 );
echo '<img src="';
echo $image_path;
echo '" />';
}
}
}
The function you're using contains buffer manipulation functions (ob_start() and ob_end_clean()), which and are most likely conflicting with something else in your template (probably something that's also using them, usually plugins).
Unless you're also searching for images inserted as post meta (which I assume you're not), you will find all images in a post inside the $post->post_content property. You can use preg_match_all on it to find the images.
You should pass the post content to your function as a parameter instead of accessing the global $post object. This will make things more reliable and easier to debug. (Global objects are generally frowned upon in PHP development, which is something to keep in mind if you plan to use PHP more generally than just for WordPress.)
This is how your function could look:
function getAllImagesInPost($postContent) {
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $postContent, $matches);
// This regex function searches for <img> tags and uses a capture group
// for the image source url. It will search the whole supplied string,
// in this case post content. It returns results into the $matches variable.
$image_tags = $matches[0];
// This is an array containing all the regex matches.
$image_urls = $matches[1];
// This is an array containing all the capture group matches.
return implode('',$image_tags);
// implode() creates a string from an array of strings/numbers.
}
In your template file, the usage depends on whether or not you're in the loop. In the loop you can work with the $post object and use either $post->post_content or get_the_content() (both will get you the post content as a string):
echo getAllImagesInPost(get_the_content());
Outside of the loop you will first need to get the post (e.g. get_post($somePostID);).
Addition 1:
You must pass a variable to the function, so you can't use WordPress template functions such as the_content(). These echo directly to the page and return true or something else useless (in this case, anyway). General WP rule is that if it starts with the_ it echoes, if it starts get_the_ it returns.
Addition 2:
To display only the images from your post and no other content, you simply don't use any other content template tag in your template file. (E.g. no the_content()).
Addition 3:
I suspect your problem isn't caused by the function itself but by how/where you're calling it. But if this function does cause changes in your layout, it's 99% the regex returning broken HTML (not likely but possible, depending on your input).
The way to debug this is to inspect the page with browser dev tools and look for broken tags or other weirdness. If you see that, you need to find what exactly happens by testing the function on various input. Feed it a hard-coded string, make a test post with just text/image/anything else, until you know what's happening. If you determine that the regex is the culprit, you can try fixing it yourself or search the internet for a better one (no judgement, regex is pain).
I want to change the output for get_header_image_tag function to output the exact HTML that I want to. I also want to be able to add data to the output such as new srcset that have not been covered...
I have tried to use apply_filters get_header_image_tag to test it out but couldn't get it to work:
apply_filters('get_header_image_tag', "<img src>", get_custom_header(), ['url' => 'test']);
echo get_header_image_tag();
I am pretty convinced that my understanding of how the apply_filters works might be the issue there... I've been reading about it but I can't get my head around the parameters. Most of the examples I have found online used only a hook and a single value.
The way I understood it, I want the output to be <img src=url> by using the data in get_custom_header() and replacing the URL attribute with 'test'.
However, what is being outputted is the default get_header_image_tag. I've also tried to directly echo the apply_filters:
echo apply_filters('get_header_image_tag', "<img src>", get_custom_header(), ['url' => 'test']);
But then, only <img src> is outputted...
You are totally right, it is your understanding on how to use wordpress filters that is the issue :)
You are applying a filter when using apply_filters(). To add your own filter to the get_header_image_tag hook you have to use add_filter(). Here is an example on how adding a filter should look like:
// define the get_header_image_tag callback
function filter_get_header_image_tag( $html, $header, $attr ) {
// make filter magic happen here...
return $html;
};
// add the filter
add_filter( 'get_header_image_tag', 'filter_get_header_image_tag', 10, 3 );
Here is an example on how you can control the full output of get_header_image_tag:
function header_image_markup($html, $header, $attr) {
return '<figure><img src="'.$attr['src'].'" width="'.$attr['width'].'" height="'.$attr['height'].'" alt="'.$attr['alt'].'" srcset="'.$attr['srcset'].'" sizes="'.$attr['sizes'].'"></figure>';
}
add_filter('get_header_image_tag', 'header_image_markup', 20, 3);
But, what version of WP are you using? I'm pretty sure srcset is supported in the get_header_image_tag() as it appeared when I was using it just now.
I'm by no means a PHP guru and have been given the task of updating a WordPress site. I've managed to get my php-includer plugin to work so that I can use a shortcode within the content area which the editor can then reference a stand-alone file which will then get injected into the main page.
The code I'm using is this:
<?php
function PHP_Include($params - array()) {
extract(shortcode_atts(array(
'file' => 'default'
), $params));
ob_start();
include(get_stylesheet_directory() . "/inc/$file.php");
return ob_get_clean();
}
add_shortcode('phpinclude', 'PHP_Include');
?>
I've saved this as a plugin and then using [phpinclude file='testfile'] in the content on a blog post. While the external testfile.php is included into the content, what I am after is a way of targeting parameters.
So for example, if the page held 4 images, I could specify in a parameter how many I would want to display. The editor could then add the following code [phpinclude file='testfile' images=2]. This would then display only 2 images instead of the default 4 images.
Does anyone know of a way to do this with the code I've shown above or point me in the right direction?
If you just add that parameter to your shortcode:
<?php
function PHP_Include($params - array()) {
extract(shortcode_atts(array(
'file' => 'default',
'images' => 4
), $params));
ob_start();
include(get_stylesheet_directory() . "/inc/$file.php");
return ob_get_clean();
}
add_shortcode('phpinclude', 'PHP_Include');
?>
then the $images variable containing the number of images to display is available to the code of your included PHP file, which can act upon it accordingly.
I'm pretty new to PHP so perhaps lacking some basics so here goes.
For WordPress I've a function for replacing some text from a TablePress table. The function worked fine when I used code along these lines:
function replace_stuff($text) {
if (is_front_page() || is_page('2611') || is_child('2611')) {
$replace_magic = array(
//text to search => text to replace
'dog' => 'cat',
'mouse' => 'elephant'
);
}
$text = str_replace(array_keys( (array)$replace_magic), $replace_magic, $text);
return $text;
}
add_filter('tablepress_table_output', 'replace_stuff');
So in that example dog would be displayed on the frontend as cat & mouse as elephant.
But now I would like to complicate things & create the strings to replace by querying fields from all posts in a custom post type "drivers".
I have come up with something like this, with the aim of finding any text that matches the post title & replacing with text from a custom field (of all posts from my 'drivers' custom post type), but it doesn't do anything!
function replace_stuff($text) {
if (is_front_page() || is_page('2611') || is_child('2611') || get_post_type() == 'drivers') {
query_posts('post_type=drivers');
if (have_posts()) :
while (have_posts()) : the_post();
$profilenat = get_post_meta($post->ID, 'driver_nationality', true);
$profiletitle = get_the_title();
$replace_magic = array(
//text to search => text to replace
$profiletitle => $profilenat
);
endwhile;
endif;
}
$text = str_replace(array_keys( (array)$replace_magic), $replace_magic, $text);
return $text;
}
add_filter('tablepress_table_output', 'replace_stuff');
Could anyone advise me please?
Many thanks.
Firstly I think you need to replace
$replace_magic = array(
with
$replace_magic[$profiletitle] = $profilenat
Currently, if everything else works, then for every driver you are completely replacing the contents of $replace_magic with a new array, which just has that driver's details. Instead you want to add a new item to the existing array.
Going further, with this sort of problem it can be really useful to do some quick debugging to help you narrow down where the problem might be. So here it would be useful to know if the problem is really with your str_replace, or if it's actually with the code above it.
Debugging in Wordpress is worth a read, and having done that you can use error_log to output some details to debug.log inside your wp-content directory.
Before your str_replace, doing
error_log(print_r($replace_magic));
Would tell you if your query loop has worked as you intended or not.
If it hasn't, you might then put a log statement inside the loop. This will tell you if the loop contents are being executed at all (in which case the problem is with your code inside the loop), or not (in which case the problem may be with your query).
Additionally, if you haven't already I would recommend checking the WordPress Codex on query_posts. query_posts manipulates the main Wordpress query, and may give you some really unexpected results used inside a filter like this. At least consider WP_Query - and check the note about wp_reset_posts.
Hope that helps - apologies if some of it is stuff you already considered, but as you mentioned you're quite new to PHP I hope it's useful.
+269: [critical] Potential problem: drupal_set_message
http://api.drupal.org/api/function/drupal_set_message/() only accepts
filtered text, be sure all !placeholders for $variables in t
http://api.drupal.org/api/function/t/() are fully sanitized using
check_plain http://api.drupal.org/api/function/check_plain/(),
filter_xss http://api.drupal.org/api/function/filter_xss/() or
similar.
Which pertains to this code:
drupal_set_message(t('Batch complete! View/Download !results', array(
'!results' => filter_xss(l(t('simple results'), file_create_url($filename))),
)), 'info');
What's going wrong?
The method you're using is under the 'DO NOT DO THESE THINGS' portion of Dynamic or static links in translatable strings. You need to change it to one of the approved methods. For reference:
<?php
// DO NOT DO THESE THINGS
$BAD_EXTERNAL_LINK = t('Look at Drupal documentation at !handbook.', array('!handbook' => ''. t('the Drupal Handbooks') .''));
$ANOTHER_BAD_EXTERNAL_LINK = t('Look at Drupal documentation at the Drupal Handbooks.');
$BAD_INTERNAL_LINK = t('To get an overview of your administration options, go to !administer in the main menu.', array('!administer' => l(t('the Administer screen'), 'admin'));
// Do this instead.
$external_link = t('Look at Drupal documentation at the Drupal Handbooks.', array('#drupal-handbook' => 'http://drupal.org/handbooks'));
$internal_link = t('To get an overview of your administration options, go to the Administer screen in the main menu.', array('#administer-page' => url('admin')));
?>