I am trying to add a block to all my posts, i am using the_content Hook to add the shortcode, the code below adds the shortcode in the end of the content, is there something i can do to put the shortcode for example after 25% of the content?
add_filter( 'the_content', 'my_shortchode_in_single_page' );
function my_shortchode_in_single_page($content){
if(is_single())
return $content . do_shortcode('SHORTCODE HERE');
return $content;
}
Actually we can't found 25% of content but we can put it after a certain number of p tag. you can 0 to which number of p tag in content.
add_filter('the_content', 'ak_the_content_filter', 10, 1);
function ak_the_content_filter($content)
{
$parags = explode('</p>', $content);
$parags[0] .= '<br>'.do_shortcode('[SHORTCODE HERE]');// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
Related
I am trying to add a block to all my posts, i am using the_content Hook to add the shortcode, the code below adds the shortcode after the specified element $parags[3] (the block shows after the fourth paragraphe), is there something i can do to change the order and put the shortcode before the specified element (the other way around)
add_filter('the_content', 'ak_the_content_filter', 10, 1);
function ak_the_content_filter($content)
{
$parags = explode('</p>', $content);
$parags[3] .= do_shortcode('[SHORTCODE HERE]');
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
How to change the position of a line in PHP?
I'm trying to add a custom text line in the woocommerce product description tab.
But it's coming at the bottom of the tab. I want the line at the top of the page. How can I do that?
I'm using this code.
add_filter( 'the_content', 'customizing_woocommerce_description' );
function customizing_woocommerce_description( $content ) {
// Only for single product pages (woocommerce)
if ( is_product() ) {
// The custom content
$custom_content = '<p class="custom-content">' . __("This is the last line in the description", "woocommerce").'</p>';
// Inserting the custom content at the end
$content .= $custom_content;
}
return $content;
}
$content .= $custom_content means $content = $content . $custom_content;
You probably need this if I understood correctly:
$content = $custom_content . $content;
So, complete code:
// Only for single product pages (woocommerce)
if ( is_product() ) {
// The custom content
$custom_content = '<p class="custom-content">' . __("This is the last line in the description", "woocommerce").'</p>';
// Inserting the custom content at the end
$content = $custom_content . $content;
}
return $content;
I'm working on adding social media buttons on all posts. I am using this code:
function wintersong_custom_buttons($content) {
$new_content = 'Button HTML here.';
$content .= $new_content;
return $content;
}
add_filter('the_content', 'wintersong_custom_buttons');
I have tried to add is_single but then the buttons only appear when you view a post.
I want them to appear only...
when the user is viewing all posts
when the user has clicked on a post and is viewing all content of it
How can I do that?
solution:
function wintersong_custom_buttons($content) {
if( ! is_page() ) {
$new_content = 'Button HTML here';
$content .= $new_content;
$content = preg_replace('/\s+/', ' ', trim($content));
}
return $content;
}
add_filter('the_content', 'wintersong_custom_buttons');
I'm using the multiple post thumbnails plugin for Wordpress and am trouble finding a way to remove the default html attributes wordpress seem to generate? When you view the html source it comes up with;
<img width="600" height="450" src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="hotplate2thumb" />
I want to remove the width and height because that's invalid code. And I want to remove the class too, to make the site look less wordpressy.
Ideally it should generate as:
<img src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" alt="hotplate2thumb" />
I've already got this in the functions.php
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
But that only works for wordpress' defult post thumbnails and not the multiple/secondary one.
I also tried implementing the code like so on the template:
<?php
if (class_exists('MultiPostThumbnails')):
echo "<img src=\"".MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image') . "\" alt=\"\" />";
endif; ?>
But that still renders a second image for all posts even if I don't have one set, so then I get broken images appearing in the theme. Not all my posts use a second image, only a few.
Thanks
Okay I solved it by adding
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
to this function
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
I have a Wordpress site and I want to remove the block quotes from the post and put just the regular text. (also want to remove any images that are in the text, I just want the regular text)
This code does the OPPOSITE of what I want - takes out the block quotes and posts that. I want it to post the other text and NOT the block quote.
<?php
// get the content
$block = get_the_content();
// check and retrieve blockquote
if(preg_match('~<blockquote>([\s\S]+?)</blockquote>~', $block, $matches))
// output blockquote
echo $matches[1];
?>
What you need is a content filter. Add the following to your functions.php file
add_filter( 'the_content', 'rm_quotes_and_images' );
function rm_quotes_and_images($content)
{
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = preg_replace("/<img[^>]+>/i", "", $content);
return $content;
}
Try this
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
return $content;
}
Just add this to your code:
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
echo $content;
The way wali hassan sayed is to add following code to your function.php:
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
return $content;
}
This overrides the default "the_content()" function so in you page template you only need to call:
the_content();