I'm new to WordPress and we have a form that people can fill out to submit posts onto our site.
We used Advanced Custom Fields plugin to fill out all the necessary information we needed. We have on field that is story_description.
I would like to make that story_description to use all the excerpt filters (length, etc) and save it under the post_excerpt in the wp_posts table.
How would I go about having that save in that table with the custom length and the other specific in the excerpt that are defined? I'm new to all the filters and actions.
Is there a way to just overwrite is and treat is like a normal excerpt when the post is saved?
I appreciate any responses.
Your first need to add the following to allow excerpt generation outside of the loop (this go in your theme functions.php):
function rw_trim_excerpt( $text='' )
{
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
return wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
add_filter('wp_trim_excerpt', 'rw_trim_excerpt');
This function comes from jlengstorf answer.
Then when you insert your post do the following:
wp_insert_post(array(
// ... other stuffs ...
post_excerpt => apply_filters('get_the_excerpt', $story_description)
));
This will filter your $story_description string with all the excerpt WP functions.
Related
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.
I've used the Advanced Custom Fields plugin to create a new Custom Field in Wordpress. My aim now is to move all Download Links from "the_content", to the newly created Custom Field "the_field('download_link')". The issue is that I have over 10,000 posts to modify. I was wondering if there is a quick way of doing this, rather than manually moving the download link for each post?
Please see the images below for an idea of what I am trying to achieve.
Before | After
One hurdle is that all content is saved in the "wp_posts" table, where the custom field content is saved in the "wp_postmeta" table.
The content saved in the "download_link" custom field, looks like this in the "wp_postmeta" table:
(8214, 2282, 'download_link', '<div class=\"source\"><img src=\"https://www.google.com/image.png\"></div>'),
(8215, 2282, '_download_link', 'field_5cffd35335ce3'),
(8220, 2280, 'download_link', '<div class=\"source\"><img src=\"https://www.google.com/image.png\"></div>'),
(8221, 2280, '_download_link', 'field_5cffd35335ce3'),
(8226, 2278, 'download_link', '<div class=\"source\"><img src=\"https://www.google.com/image.png\"></div>'),
(8227, 2278, '_download_link', 'field_5cffd35335ce3'),
Can this be done at all? Or is the only real way to achieve this is by moving the download links manually?
Thanks in advance for your help.
It can be done automatically if the format of your download link is always the same.
Backup your database
Create a php file named my-cleanup.php.
Loop over all products, grab the link from the description and move it to a post_meta.
Launch the file with WP-CLI:
wp eval-file my-cleanup.php
Sample code
// Select the posts you want
$args = array(
'post_type' => 'post',
'post_status' => 'any',
// -1 means all posts
// For debug, set this to a small number
'posts_per_page' => 3,
);
// Retrieve posts
$query = new WP_Query( $args );
$posts = $query->posts;
// Pattern to match
// [wpdm_package id='90228']
$pattern = "/(\[wpdm_package id='\d+'\])/";
// Process posts one by one
foreach ( $posts as $post ) {
echo "Processing " . $post->post_title . "\n";
$post_args['ID'] = $post->ID;
// Get the shortcode from the post content
preg_match( $pattern, $post->post_content, $matches );
// Do we have a match?
if ( count( $matches) > 0 ) {
// Retrieve shortcode
$shortcode = $matches[0];
// Convert shortcode, maybe add some HTML
$link = do_shortcode( $shortcode );
// remove shortcode from description
$post_args['post_content'] = preg_replace( $pattern, '', $post->post_content );
// Update post
wp_update_post( $post_args );
// Create the post_metas
add_post_meta( $post->ID, '_download_link', 'field_5cffd35335ce3', true );
add_post_meta( $post->ID, 'download_link', $link, true );
}
}
Notes
Backup your database
Test on a few posts to get confidence in your code. You can restore the previous revision in the editor.
Then try on 100, then 1000. You will often find small differences in the way posts are entered.
This might take hours to run on 10.000 posts, which is why you need a WP-CLI command.
I have a custom theme where when I upload an image with caption, the caption shortcode is shown on the page: [caption id="attachment_109"].... Using either of WP's default themes, I see no issue. On inspection, WP uses the_content(), link, so I need to do some stripping. I get my posts with get_page_by_path():
<?php
$post = get_page_by_path( $current_page->post_name, OBJECT, 'service' );
// I assume this would work
$content = the_content($post->post_content);
//Blank page:
echo $content;
Echoing $post->post_content shows the caption shortcode as mentioned above. How to get rid of if it? BTW, I need the caption values.
You can get the post content like this
$post = get_post(123); //pass the id of the post
$content = $post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
or
$content=apply_filters('the_content', get_post_field('post_content', 123)); //123 is the post id
after that just strip the shortcode also you can check if the post is having the shortcode in it or not by has_shortcode
I would like to display posts within other posts and pages in WordPress without the use of a plugin. I've searched this site (and others) but haven't found a definitive answer to what I am looking for.
I'd like to display a short-code within posts and pages in this fashion:
[insert_post id="99"]
where 99 is the ID of the post (or page, if possible).
I'd like to pull in the following:
title,
the content (first 30 words),
read more button,
featured image (thumb).
Any tips will be appreciated.
You need to create the shortcode first. This code goes in your functions.php. Here is a basic example to achieve what you are trying to achieve:
function insert_post_shortcode_function( $atts ) {
$attributes = shortcode_atts( array(
'id' => '' // attribute_name => default_value
), $atts );
$return = '';
$post = get_post($attributes['id']);
$return .= get_the_post_thumbnail($post->ID);
$return .= $post->post_title;
$return .= $post->post_content;
$return .= 'Read more';
return $return;
}
add_shortcode( 'insert_post', 'insert_post_shortcode_function' );
Now you can use [insert_post id="x"] in your post's content.
More on creating shortcodes - https://codex.wordpress.org/Shortcode_API
I have a small form on my website, which I created using Jetpack plugin (it has built-in contact form creator).
I want to pull options for select input from my custom-post-type "artist" (using the titles of the posts). Is there a way to do it?
[contact-field label='Artist' type='select' required='1' options='i want my titles go here separated by commas plus "other" option'/]
The code for the field looks like this. I believe I need to do some php+jquery stuff in this page template, but I can't seem to get it.
With some creativity, yes, it's possible :)
We create another Shortcode to create a "virtual" JetPack shortcode.
I tested this using the default post post_type.
add_shortcode( 'my-jet-form', 'so_14003883_jet_form' );
function so_14003883_jet_form( $atts, $content )
{
// Query our post type, change accordingly
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'post_status' => 'publish'
) );
// Build an array of post titles
$titles = array();
foreach( $posts as $post )
{
$titles[] = $post->post_title;
}
// Convert array into comma sepparated string
$posts_select = implode( ',', $titles );
// Make JetPack shortcode
$return = do_shortcode( '[contact-form][contact-field label="Name" type="name" required="1"/][contact-field label="Artist" type="select" options="' . $posts_select . '"/][/contact-form]' );
return $return;
}
Usage:
adjust the desired post type
adjust the do_shortcode part to suit your original shortcode
put the shortcode [my-jet-form] wherever you want
voilĂ