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;
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 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;
}
I would like to modify a posts content with the "the_content" filter.
Inside this filter I would like to load another posts content.
This is my code:
add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ){
if ( !is_singular( 'my-custom-post-type' ) ){
return $content;
}
ob_start();
echo '<p>SOME HTML BEFORE THE CONTENT</p>';
echo $content;
echo '<p>SOME HTML AFTER THE CONTENT</p>';
$module = get_post( 12345 ); // load specific post
echo apply_filters( 'the_content', $module->post_content );
echo '<p>SOME MORE HTML</p>';
$html = ob_get_contents();
ob_end_clean();
return $html;
}
Sadly this creates a 500 Internal Server Error.
I guess because I created an endless loop. Do you have any idea how to get the formatted content of another post inside the "the_content" filter?
Thanks :-)
Jan
EDIT
A bit more details: I created a custom post type called "sidebars" where I edit the content with a page builder. I would like to add this sidebars with PHP.
SOLUTION
With the "Elementor" page builder you can do this:
add_filter( 'the_content', 'my_content_filter' );
function my_content_filter( $content ){
if ( !is_singular( 'my-custom-post-type' ) ){
return $content;
}
ob_start();
echo '<p>SOME HTML BEFORE THE CONTENT</p>';
echo $content;
echo '<p>SOME HTML AFTER THE CONTENT</p>';
$elementor = \Elementor\Plugin::instance();
echo $elementor->frontend->get_builder_content( 12345 );
echo '<p>SOME MORE HTML</p>';
$html = ob_get_contents();
ob_end_clean();
return $html;
}
I recently have removed the excerpt function from Wordpress and made my own so I can allow some HTML to be shown in the excerpt. I made the changes in a child theme functions file.
It was working fine on the front-end but after that I had problems on the back-end of Wordpress. In /wp-admin/edit.php in an excerpt of posts I get the post excerpt with all the HTML tags so it was really hard to read the actual content of post.
To fix that I did a change in the Wordpress core file /wp-admin/includes/class-wp-posts-list-table.php.
I removed content from line 1037.
echo esc_html( get_the_excerpt() );
the function esc_html
How can I make this change permanent so that after a possible update of Wordpress, the change will not be lost?
Is this change safe? I will have a lot of users in my back-end of Wordpress.
UPDATE MY QUESTION
add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
function wpse_allowedtags() {
// Add custom tags to this string
return '<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>,<div>,<wbr>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
//$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' ' . ' » ' . sprintf(__( 'Read more about: %s »', 'wpse' ), get_the_title()) . '';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
//$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
// Extra filter to remove the above text from excerpt
$badwords = array(
'< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
'< ?xml encoding="utf-8" ?>',
);
foreach ( $badwords as $badword ) {
$wpse_excerpt = str_replace( $badword, '', $wpse_excerpt);
}
//End extra filrer
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
I used this function to add html tags in excerpt.
I after updated the excerpts in database. And i get the result that i wanted.
But i get this in backend.
problem backend
So a fast way to get away with the problem was to go to /wp-admin/includes/class-wp-posts-list-table.php and remove the function esc_html from this line echo esc_html( get_the_excerpt() ); where the excerpt is generated for back-end.
So my question is not about how to allow html tags in excerpt, I already did that, but how to make that change permanent.
My back-end after that change. Back-end after
That is what i want to do.
Don't ever change WP core... You'll lose the ability to update it and your system will be vulnerable.
You can create plugins to change the excerpt behavior, you can create your own functions and use it in your theme templates...
There is a lot of ways better than to t
Never change the core of the WordPress. As Artur Luiz Oliveira mentioned you will lose those changes the first time WordPress will update, and you should be aware that it will do that automatically once the version becomes very dated if you like it or not.
To do what you are looking for I would suggest you look into this question.
Might be more coding involved but this will stick and won't be affected when the WordPress version updates.
I want to insert some HTML tags in a PHP variable, but it results in the PHP variable value only.
It gets the the_subtitle() value and inserts it in the PHP variable and finally adds it to the first part of $content. Why doesn't the HTML tags?
My code:
add_filter( 'the_content', 'add_before' , 20 );
function add_before($content) {
if ( the_subtitle() ){
$custom_content = '<h2 class="subtitlessss">' . the_subtitle() . '</h2><br>';
}
$content = $custom_content . $content;
return $content;
}
You're modifying the output of the_content() by using a filter. Your callback should return a value. However, the function you're using is going to output your subtitle rather than return it.
According to the plugin documentation it works in the same way as WordPress' the_title(). Initially I thought get_the_subtitle() would be more appropriate. However, that isn't the case. Tell the_subtitle() not to output instead (third argument).
function add_before( $content ) {
// Check if the subtitle has been set and assign the value to $subtitle
// Saves us having to call the same function again.
if ( $subtitle = the_subtitle( '<h2 class="subtitlessss">', '</h2><br />', false ) ) {
// Prepend to the content.
$content = $subtitle . $content;
}
// ALWAYS return content.
return $content;
}
add_filter( 'the_content', 'add_before', 20 );
The code you have will always run this line:
$content = $custom_content . $content;
Could that be the issue? Try only modifying $content if the_subtitle() returns a true value, like this:
add_filter( 'the_content', 'add_before', 20 );
function add_before( $content ) {
if ( the_subtitle() ) {
$custom_content = '<h2 class="subtitlessss">' . the_subtitle() . '</h2><br>';
$content = $custom_content . $content;
}
return $content;
}
If you are only seeing the value of $content as your code stands currently, then that means the_subtitle() isn't returning a true value so there is no value in $custom_content. This should throw a Notice: Undefined variable message. Does your wp-config have this line in it?
define('WP_DEBUG', true);
After researching the_subtitle()
OK, so it seems like the_subtitle() takes the same parameters as the native WordPress function the_title as the documentation says here:
Parameters Just like WP's built-in the_title() method,
the_subtitle() tag accepts three parameters:
$before (string) Text to place before the subtitle. Defaults to "".
$after (string) Text to place after the subtitle. Defaults to "".
Therefore, if you want HTML to come before and after the_subtitle, pass it as a parameter:
the_subtitle( '<h2 class="subtitlessss">','</h2><br>' );
To check if the function exists
Also, maybe we should be checking if the_subtitle exists instead of calling it in the if statement? Like so:
if ( function_exists( 'the_subtitle' ) )
The updated code for the entire excerpt would be this:
<?php
add_filter( 'the_content', 'add_before', 20 );
function add_before( $content ) {
if ( function_exists( 'the_subtitle' ) ) {
$custom_content = the_subtitle( '<h2 class="subtitlessss">','</h2><br>' );
$content = $custom_content . $content;
}
return $content;
}
I have a slight issue. I have managed, with the help of someone one stackoverflow to write a code to add dynamic content to all my wordpress posts. The code is now in my functions.php file and is as follows.
function add_after_post_content($content) {
global $post;
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$post_categories = wp_get_post_categories( $post->ID );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
foreach($cats as $c){
$content .= $c['name'];
}
$content .= ' category by <strong>Free Wallpapers</strong> on '. $post->post_date . '. <br /><br />';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
The issue is that some of my older posts already have content in them and hence both the old content as well as the new are displayed beneath them. whereas I would like this to be the new content/description for all posts. Currently in my content-single.php I believe this is the line which calls the original post content plus the code I have mentioned above.
<?php the_content(); ?>
I would like to wither remove or modify my content-single.php/code added to functions.php file so that only the new code displays the dynamic description, and the original post content is not displayed. i.e. putting it simply if the entire new code could be labeled content2 and the content-single.php code will output this content2 only. I have tried simply moving the code from functions.php to replace
<?php the_content(); ?>
in the content-single.php but this gives me all kinds of errors.
Any help on this issue would be greatly appreciated.
Best Regards
Remove the dot after $content.
$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
Replace above line with following
$content = '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';