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.
Related
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);
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!
I'm working on a WordPress site that has been using a plugin to grab amazon product images using a shortcode.
You simply insert the asin of a book into a shortcode, like this:
[amazon template=image&asin=B00FYY53B8]
When the post loads, the shortcode is converted into the actual image HTML using the URL from amazon.... so the example above would return
http://ecx.images-amazon.com/images/I/71kIifYTccL._SL1500_.jpg
I'm using another custom plugin to build out the content for posts, and so far am just using this shortcode as part of the post content. I would like to do some other things, such as set the featured image, etc. and need that URL.
I've tried
echo do_shortcode( '[amazon template=image&asin=B00FYY53B8]' );
But it doesn't return anything. Is there a way to "execute" shortcodes in a plugin and save the output?
Ultimately, I would also like to scale this functionality so I can replace other/old shortcodes with their HTML output, and save it back to the post, essentially eliminating the use of some shortcodes in posts.
I have a demo for add a short code in wordpress. I hope it help.
add_action('plugins_loaded','MOD_load');
function MOD_load() {
short_enable_form();
}
function short_enable_form(){
if(!function_exists('add_shortcode')) {
return;
}
add_shortcode('form_mod' , array(&$this, 'out_put_form'));
}
public function out_put_form(){
return 'HTML TEXT';
}
You can include this code in functions.php. And call this function in content-singular.php or the similar file in your theme.
function updatesc_database( $post ) {
if ( empty($post) ) global $post;
if ( empty($post) || ! isset($post->post_content) ) return false;
$content = $post->post_content;
$shortc1="amazon template=image&asin";
$shortc2="B00FYY53B8]"; //insert the number variable here
$shortwh=$shortc1.$shortc2;
$replace = do_shortcode($shortwh);
$content = str_replace( $shortwh, $replace, $content );
return $content;
}
I have a Wordpress site that outputs content from individual blog posts with the_content()
Blog posts all consist of two things, a small gallery and some text:
<div class="gallery"><img></img>Blah Blah</div>
<p>Text</p>
<p>Text</p>
I'd like to split the gallery and the text and output the gallery in a div on the left and the text in a div on the right like this:
<div id="left">GALLERY CONTENT</div>
<div id="right">TEXT CONNTENT</div>
I have tried to do this with strip_tags(the_content(), '<p>') but this does not - it continues to output everything including the gallery.
What is the correct way to do this?
It is not really clear to me what you really want to do , and on to of it , you included some (very little ) source code from output, but to get a real shot at answering you need to include the relative code from the template file.
( And +1 for understanding alone that you should NOT TOUCH YOUR CORE FILES )
Anyhow, I suspect that you only want to disable the auto P generated by wordrpess , so try
remove_filter('the_content', 'wpautop');
(add to functions.php in theme.)
alternatively , you could use
add_filter('use_default_gallery_style', '__return_false');
Which will just "reset" the gallery styling .
or even filter your own gallery styles , which allows you to target them better.
add_filter( 'gallery_style', 'my_own_gallery_style', 99 );
function my_own_gallery_style() {
return "<div class='gallery'>"; // put your own
}
If it does not produce the right output for you, please include more specifics and / or more code .
There are of course more advanced ways to handle this , but Without more info it is difficult to target .
For example you can create your own gallery style by removing the original shortcode function, and then adding your own, but those are a bit more advanced techniques.
// deactivate WordPress function
remove_shortcode('gallery', 'gallery_shortcode');
// activate your own own function
add_shortcode('gallery', 'my_own_gallery_shortcode');
// the own renamed function
function my_own_gallery_shortcode($attr) {
...
}
Now on the other hand , If you want to "catch" some parts of 'the_content' and display it in a loop in a different manner , you can always use a different technique, like described HERE on another answer .
You are using the_content which displays the content instead of returning it.
Change your code to
strip_tags(get_the_content(), '<p>')
I had this exact same problem a while ago. Here's what I did (in single.php, which is where I had the problem):
if ( get_post_format() == 'gallery' ) :
$content = get_the_content();
$gallery_regex = '/\[gallery.*]/s'; //identify the [gallery] tags within the content
//get gallery code
$gallery = preg_match($gallery_regex, $content, $matches);
$gallery = $matches[0];
//remove gallery from content
add_filter('the_content', function($content){
$gallery_regex = '/\[gallery.*]\s*/s';
return preg_replace($gallery_regex, ' ', $content);
});
endif;
Basically, I used regex to remove the gallery tags from the content.
$gallery still contains the shortcode. We can't just randomly display it, or it'll actually show the shortcode. We need to execute it, which will show the output:
if ( get_post_format() == 'gallery' ) {
echo '<div id="left">'. do_shortcode($gallery) .'</div>';
}
Your content no longer contains the gallery, so you can do this:
<div id="right"><?php the_content(); ?></div>
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);