Disable Wordpress short tag - php

I want to modify the layout for a Wordpress short tag. The tag I want to modify is the tag which will break a single post into multiple pages.
In my theme I need to disable that functionality and wrap each section in a div.
I know it's possible to add filters to modify short tags but clearly i'm doing something wrong. The function below doesn't seem to replace the short tag and i'm still getting a paginated list.
Can anyone suggest a solution to replace the short tags?
add_filter( 'the_content', 'reformat_lists' );
function reformat_lists($content){
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
str_replace($f,$r,$content);
return $content;
}

It's likely that your post query is calling setup_postdata, so that <!--nextpage--> has already been replaced before you get the chance, so you might have to use another filter or figure out what Wordpress is inserting. You could get at the_content before setup_postdata if you used get_posts instead of WP_query. Ideally, you could find a filter on the_content that occurs before this, but not before DB write, but I can't seem to find anything to do job.
Its not beautiful, in that it is destructive (replaces the tag before saving to the database) rather than just prior to printing, but this might work for you:
function reformat_lists($content){
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
$content = str_ireplace($f,$r,$content); //don't forget to pass your replaced string to $content
return $content;
}
add_filter( 'content_save_pre', 'reformat_lists');
EDIT: Even better, if you get the global $post, you CAN grab the unfiltered content. Try the below - I added a </div> onto the end of the content to close the one we're inserting so it wont break your layout. Grabbing the global $post might not work in all contexts, so I leave it up to test in your setup.
function reformat_lists($content){
global $post;
$content = $post->post_content;
$f = '<!--nextpage-->';
$r = '<div id="cmn-list">';
$content = str_ireplace($f,$r,$content) . '</div>';
return $content;
}
add_filter( 'the_content', 'reformat_lists', 1);

Related

Get first word from Wordpress page title

Edit
Is this code ok?
It works on a dummy website, but I'm afraid to not break a live website.
If used for links, is there a problem if links have capitalization?
function shortcode_page_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'shortcode_page_title', );
function shortcode_title_first_word( ){
$title = get_the_title();
$title_words = explode(' ', $title);
return $title_words[0];
}
add_shortcode( 'title_first_word', 'shortcode_title_first_word', );
Thanks, #ADyson, for the resources.
Initial post
I can't code :(
I'm using Sepster's solution to get the page title.
function shortcode_page_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'shortcode_page_title', );
How can I insert another step and select only the first word of the title?
I've seen the explode function and array selection, but I don't know how to implement them.
Thank you!
If you still want to use the shortcode as the way to insert the title wherever you need it, then all you need is to combine the 2 functions above and insert it on the bottom of your functions.php file (preferably on a child theme).
function first_word_from_title( ){
$title = get_the_title(); // Retrieves the title
$title_words = explode(' ', $title); // Transforms the title into an array composed of each separate word
return $title_words[0]; // Returns the first element of the array
}
add_shortcode( 'page_title', 'first_word_from_title');
I don't think you need to worry about the above code causing a crash as it is only run when you insert the shortcode and not on every page load.
You can always test it first on a draft post and see if you get an error.

Only target posts and not pages when using the_content() PHP function with WordPress

I'm having trouble changing the posts display in my WordPress website. So far, the posts can display a title and text content, and I would like to display tags, categories and an image. I added the following code in functions.php and it actually displays the p tags, but around the content, and not in it. Also, it is displayed on all pages of my website, while I just want to add these HTML tags inside the posts.
So,
How can I put the tags inside the_content() and only display the custom HTML in posts?
I hope my question is clear, I starting learning PHP a few days ago, sorry!
Thank you so much in advance for you help!
The code :
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);
Not totally sure what you want for the "in it" part but I can help with conditionally applying your code to posts only... See below:
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
// if not a post then return the $content as is
if ('post' !== get_post_type()) {
return $content;
}
// must be a post if we get here so lets do something with it
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);

WordPress: Add text after (or before) previously posted content based on publish date

I've been searching for a while now with no certain answer. I'm looking to append text to a WordPress site for prior posts that is moving from one domain to a new one and retaining content.
So what I want to do is, add "This article was originally posted at xyz.com." to all posts that were posted before today's date.
Right now this could be done through the database, or a WP functions filter, I'm okay with either option as long as it is long lasting.
Any suggestions on how to go about this would be appreciated?
You can use the_content filter that like this:
add_filter( 'the_content', 'old_wp_content' );
function old_wp_content( $content ) {
if( get_the_date('Y-m-d') < "2017-02-28" ) {
$content = "<p>This article was originally posted at xyz.com.</p>" . $content;
}
return $content;
}
This filter gets fired when you call the_content() of a post. with the_content filter you can adjust the return value of the the_content() function.
I finally got it. That extra "if" before the get_the_date statement and possibly the double quotation marks (swapped for single) wrapping the p tags for the inserted text were the culprits. The following code works:
function old_wp_content( $content ) {
if (get_the_date('Y-m-d') < '2017-02-28' ) {
$content = $content . '<p>This article was originally posted at <a
rel="canonical" href="#">xyz.com</a>.</p>';
}
return $content;
}
add_filter( 'the_content', 'old_wp_content' );
#kevinvhengst, thanks again for your time and patience in helping me figure this out!

Automatically Add Default Text Into WordPress Post Editor For Existing and New Posts

I am trying to create a function that will automatically add default text into the WordPress Post Editor for existing and new posts. I already found code to add_filter to default_content and that works well for new posts, but will not have any affect on existing, published posts/pages. The new default text could be added when I press "Update" to existing posts, that would be fine.
Here is what I have so far:
function add_before_content($content) {
$content = '<p>My default content.</p>';
return $content;
}
add_action('publish_post', 'add_before_content');
add_action('update_post', 'add_before_content');
add_filter('default_content', 'add_before_content');
Thanks in advance.
Filters and actions send different parameters to their functions, so you can't necessarily use the same function for different ones. Check the filter/action references for the right parameters for each.
Also, there is no update_post action. Maybe you wanted save_post.
But actions are probably not what you want to use anyway. They are good for doing something related to the subject of the action, but to deal with content you're better off using filters.
For example, something like the following might do what you want:
function add_before_content( $content ) {
$my_content = '<p>My default content.</p>';
if (substr($content, 0, strlen($my_content)) != $my_content) {
$content = $my_content . $content;
}
return $content;
}
add_filter( 'content_save_pre', 'add_before_content', 10, 1 );

Calling WordPress get_template_part from inside a shortcode function renders template first

I have a page where I need to allow the user to enter a paragraph of text. Then after that text, insert a shortcode that will render a list of posts, then add more free-form text after that. My thought was that they should be able to insert a shortcode which will output the posts. This way they can simply add the shortcode where they wish the posts to appear.
I currently have the logic which retrieve the posts separated in its own file. Currently I include it in a page by simply using the get_template_part() function:
get_template_part('donation', 'posts');
I looked into how to create a shortcode and included the following code into my functions.php file in order to create the shortcode:
add_shortcode('donation-posts', 'fnDonatePosts');
function fnDonatePosts($attr, $content)
{
get_template_part('donation', 'posts');
}
The donation-posts.php is being properly executed and the posts are appearing, however, they are always positioned BEFORE the content and not in the location that the shortcode was placed.
I have tried removing the get_template_part() function and just output some text and that works fine. So I understand that the get_template_part() may not be the right way to do this, however, I have not been able to uncover a way to do what I am attempting to do (I am sure that there is a way... I just haven't found it).
I have tried:
include(get_template_directory(). '/donation-posts.php');
include_once(get_template_directory(). '/donation-posts.php') :
But these stopped processing once they hit the PHP code in the included file.
I have also tried:
$file = file_get_contents(get_template_directory(). '/donation-posts.php');
return $file;
But this only returns the contents of the file (as the function name indicates) which means it doesn't execute the PHP script to return the posts.
Anybody out there done this before?
You may try this, it may solve your problem because get_template_part basically reacts like PHP's require, it doesn't return but echos the content immediately where it's been called.
add_shortcode('donation-posts', 'fnDonatePosts');
function fnDonatePosts($attr, $content)
{
ob_start();
get_template_part('donation', 'posts');
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
Here is a more dynamic version where you can pass the path to the template.
function template_part( $atts, $content = null ){
$tp_atts = shortcode_atts(array(
'path' => null,
), $atts);
ob_start();
get_template_part($tp_atts['path']);
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
add_shortcode('template_part', 'template_part');
And the shortcode:
[template_part path="includes/social-sharing"]
Minimal version of the accepted answer:
function my_template_part_shortcode() {
ob_start();
get_template_part( 'my_template' );
return ob_get_clean();
}
add_shortcode( 'my_template_part', 'my_template_part_shortcode' );
where my-template.php is the file you'd like to include.
get_template_part() didn't work for me when using it in functions.php. I used locate_template() inside the ob_start and clean instead. For example:
function full_petition_shortcode( $attr ) {
ob_start();
locate_template( 'petition.php', TRUE, TRUE );
return ob_get_clean();
}
add_shortcode( 'full-petition', 'full_petition_shortcode' );

Categories