I have a function that gets the overall word count within a post.
function ic_word_count() {
global $post;
$ic_content = strip_tags( $post->post_content );
$ic_stripped = strip_shortcodes($ic_content);
return $ic_stripped;
}
I'm trying to get it to exclude all shortcodes, so basically exclude anything with square brackets and everything in between them. For example exclude [shortcode] or [This Shortcode]
Any idea on how I could add that part to the above function?
WordPress has a handy function, strip_shortcodes. Codex details here: https://codex.wordpress.org/Function_Reference/strip_shortcodes
Your function uses $post->ID but you aren't getting the global post value.
Finally you already have access to the post content inside the global $post object so just use that instead of get_post_field.
E.g. global $post;
function ic_word_count() {
global $post;
$wc_content = $post->post_content;
$ic_word_count = str_word_count( strip_tags( strip_shortcodes( $wc_content ) ) );
return $ic_word_count;
}
A smaller version:
function ic_word_count() {
global $post;
return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
}
Related
This is the current HTML but I'd like to create a shortcode to display it all...
<div class="widget" sp-sku="sku-value-goes-here"></div>
I setup a shortcode that get the SKU
function wc_get_product_sku() {
global $product;
echo $product->get_sku();
add_shortcode('product_sku', 'wc_get_product_sku');
But I'd like a new shortcode to output the entire div with the shortcode or sku in the div class.
Go ahead and give the WordPress Shortcode documentation a glance. The shortcode you have now is improper. WordPress Shortcodes are supposed to return a value, not echo or otherwise output it. If you see echo statements in a shortcode function, generally they have Output Buffering enabled, and then return the final value. (you also have a syntax error)
The following would be the proper way to implement your current function:
add_shortcode('product_sku', 'wc_get_product_sku');
function wc_get_product_sku() {
global $product;
return $product->get_sku();
}
If you want to output the HTML, just include that in the return value. If you want another function:
add_shortcode( 'product_sku_div', 'wc_product_sku_div');
function wc_product_sku_div() {
global $product;
return sprintf( '<div class="widget" sp-sku="%s"></div>', $product->get_sku() );
}
Alternatively, you could add them to the same function and use shortcode attributes to determine whether to output the whole HTML or just the SKU, something like this:
add_shortcode( 'wc_get_product_sku', 'wc_get_product_sku_function');
function wc_get_product_sku_function( $atts ) {
global $product;
$sku = $product->get_sku();
extract( shortcode_atts( array(
'wrap' => false,
), $atts, 'wc_get_product_sku' ) );
return ($wrap == true) ? sprintf( '<div class="widget" sp-sku="%s"></div>', $sku ) : $sku;
}
This would like you use [wc_get_product_sku] or [wc_get_product_sku wrap="false"] to get just the sku, and [wc_get_product_sku wrap="true"] to get the whole HTML.
Edit: to check the sku, just wrap the return statement in an if/else:
add_shortcode( 'product_sku_div', 'wc_product_sku_div');
function wc_product_sku_div() {
global $product;
if( $product && !empty( $product->get_sku() ) ){
return sprintf( '<div class="widget" sp-sku="%s"></div>', $product->get_sku() );
} else {
return '<div>NO SKU FOUND</div>';
}
}
I'm using a theme template and when i try to get post ID it returns the ID of the template not the ID of the actual single post.
the template ID is: 215
the post ID is: 1911
the following code will only output 215
function metavalue() {
global $post;
$meta = get_post_meta($post->ID, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
get_the_ID(); the_id(); $post->ID; will also output 215. i need a way to get the actual single post ID so i can get the custom field value from 'product_url'.
I've contacted support with the theme authors on this topic as well but for the time being i've found a way to work around it.
function metavalue() {
global $wp;
$url = home_url( $wp->request );
$correct_post_id = url_to_postid( $url );
$meta = get_post_meta($correct_post_id, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
I know i can use:
global $wp_query;
$wp_query->is_page = true;
For example to set the global is_page conditional to true.
Can i change the global post title in a similar way so it would effect the the_title() returned value?
To clarify things:
I need for a use of a "virtual page" case, where no post is actually loaded and i don't want to use any existing post title. just inject some custom title to the current globals so i will get it when using the_title on the current page.
To modify the title you can use a build in hook of wordpress:
function suppress_if_blurb( $title, $id = null ) {
if ( in_category(' blurb', $id ) ) {
return '';
}
return $title;
}
add_filter( 'the_title', 'suppress_if_blurb', 10, 2 );
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
Finally found it. gladly it's very simple :) but this piece of code will also create all other post vars for a "fake post/page":
$post_id = -99; // negative ID, to avoid clash with a valid post
$post = new stdClass();
$post->ID = $post_id;
$post->post_title = 'Some title or other';
$wp_post = new WP_Post( $post );
wp_cache_add( $post_id, $wp_post, 'posts' );
global $wp, $wp_query;
$wp_query->post = $wp_post;
$wp_query->posts = array( $wp_post );
$wp_query->queried_object = $wp_post;
$wp_query->queried_object_id = $post_id;
$wp_query->is_404=false;
$wp_query->is_page=true;
$GLOBALS['wp_query'] = $wp_query;
$wp->register_globals();
More details here!
I currently have this page /free-consultation-sp/ on wordpress
I'm trying to create a dynamic button on that page to go to
/free-consultation/ by just removing the last 3 characters "-sp"
here is my current code, which only return the current slug
button:
English
function.php
function the_slug_english() {
global $post;
$slug = get_post( $post )->post_name;
return $slug; }
Is there a way to do this in wordpress? Thank you very much
One simple approach is to use substr:
function the_slug_english() {
global $post;
$slug = get_post( $post )->post_name;
if (substr($slug, -3) == '-sp') {
$slug = substr($slug, 0, strlen($slug-3));
}
return $slug;
}
This will strip off -sp from the slug. The substr($slug, -3) gets the last 3 characters from the $slug.
I need to show excerpts of different lengths, so I use
function custom_excerpt($length) {
get_the_content();
... clean up and trim
return $excerpt;
}
however, I want to detect if a manual excerpt was entered in order to use that instead of the custom one. Is there a way to do this?
I tried by using
$wp_excerpt = get_the_excerpt();
But that returns the manual excerpt, and if the manual excerpt is empty, it automatically generates an excerpt of 55 characters, which doesn't help, because it will always be "true" (can't check if empty).
The reason for approaching it this way is because I have multiple excerpts on a single page (different lengths), and if the length needed is longer than the WordPress excerpt (55), I want to show my excerpt, unless a manual excerpt was written, in which case I want to show that.
It would be perfect if I could simply
if ( manual_excerpt() == true ) {
}
You need only replace the excerp_length wordpress default function, just follow the above code, then you can call this custom function and set the length:
<?php
// custom excerpt length
function custom_excerpt_length( $length = 20 ) {
return $length;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
?>
ANSWER UPDATED II
Using inside a function:
<?php
function custom_excerpt( $length = 55 ) {
if( $post->post_excerpt ) {
$content = get_the_excerpt();
} else {
$content = get_the_content();
$content = wp_trim_words( $content , $length );
}
return $excerpt;
}
?>
This is an old question but I was looking for this, ended up here and didn't see the following function in the answers. To know if a post has a custom excerpt you can use the has_excerpt function:
<?php has_excerpt( $id ); ?>
Where $id is the post id. If non is given then the current post id will be used.
Check if the post_excerpt slot in the post object is empty or not:
global $post;
if( '' == $post->post_excerpt )
{
// this post does NOT have a manual excerpt
}
To turn this into a function:
function so19935351_has_manual_excerpt( $post )
{
if( '' == $post->post_excerpt )
return false;
return true;
}