I have a shortcode [Mark] which only used as a marker for texts. And another shortcode [MarkExt] for extracting content of Marker based on the post id.
[Mark label="mrk"]Some text here[/Mark]
What I need to do is that when I invoke
[MarkExt label="mrk" id=10]
is to get the text that is enclosed by [Mark] of label mrk specified in the post id.
How can I get the [Mark ] content?
EDIT: Apologies on incomplete post . What I've done so far was
For [Mark]
function mark_shortcode($atts,$content = null){
return $content;
}
add_shortcode( 'Mark', 'mark_shortcode' );
And for [MarkExt]
function extract_shortcode($atts,$content = null){
$label = $atts['label'];
$pid = $atts['id'];
}
add_shortcode( 'MarkExt', 'extract_shortcode' );
Add following code to functions.php or plugin file.
function mark_caption_shortcode( $atts, $content = null ) {
//$content is Some text here
return $content;
}
global $mark_content;
$mark_content = add_shortcode( 'mark', 'mark_caption_shortcode' );
function extract_shortcode($atts,$content = null){
global $mark_content;
//here you will get content
$label = $atts['label'];
$pid = $atts['id'];
}
add_shortcode( 'MarkExt', 'extract_shortcode' );
Related
my problem is that: Trying to retrieve the_content with a simple shortcode function, it retrieves only the title.
Even applying another filters the result is always the same.
The content is from a page.
The function is declared in the functions.php theme file.
Using the post (page) id.
function shtcode_Func( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'id' => '5'
), $atts));
$my_postid = $atts;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
add_shortcode('shortcodePage', 'shtcode_Func');
Calling from widget with [shortcodePage id=POST_ID] (int)
Result: Prints only the title.
I tried to change the filter with 'the_post_thumbnail' and retrieved the title again.
I'm desperated :(
Thanks!!
There are several things incorrect with your shortcode function, but the main things:
You are using extract but not using anything from extract
$atts is an array, not just the id.
You are using apply_filters('the_content'). This essentially overwrites WPs built in apply_filter. You want to use add_filter, but as you can see that won't be necessary.
Here is the shortcode trimmed down with what you are trying to do:
function shtcode_Func( $atts ) {
// set up default parameters. No need to use extract here.
$a = shortcode_atts(array(
'id' => ''
), $atts);
// Use get_the_content, and pass the actual ID
$content = get_the_content('','', $a['id'] );
// This is the same
$content = str_replace(']]>', ']]>', $content);
// Return the content.
return $content;
}
add_shortcode('shortcodePage', 'shtcode_Func');
Try to use like this:
function shtcode_Func( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'id' => '5'
), $atts));
$content_post = get_post( $atts['id'] );
ob_start();
$content = $content_post->post_content;
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
$str = ob_get_contents();
ob_end_clean();
return $str;
}
add_shortcode('shortcodePage', 'shtcode_Func');
I am working on a web-service(API) where i am fetching result WP_query() function and parse that in JSON format. which will further use in android application.
The problem is the post_content i am getting with query is composed by visual composer and the whole content is in form of such tags like
[VC_ROW][/VC_ROW][VC_COLUMN]some text[/VC_COLUMN] etc.
I want to remove/strip all these shortcode from the content and retrieve only plain text from it. Is there any visual composer function through which i can achieve this thing
<?php
require('../../../wp-load.php');
require_once(ABSPATH . 'wp-includes/functions.php');
require_once(ABSPATH . 'wp-includes/shortcodes.php');
header('Content-Type: application/json');
$post_name = $_REQUEST['page'];
if($post_name!=''){
if($post_name=='services') {
$args = array(
'post_parent' => $page['services']['id'],
'post_type' => 'page',
'post_status' => 'published'
);
$posts = get_children($args);
foreach($posts as $po){
$services_array[] = array('id'=>$po->ID,'title'=>$po->post_title,'image'=>get_post_meta($po->ID, 'webservice_page_image',true),'description'=>preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $po->post_content));
}
$post = array(
'status'=>'ok',
'services'=>$services_array
);
echo json_encode($post);
}
}
?>
I want to remove/strip all these shortcode from the content and retrieve only plain text from it.
Solution that worked for me:
$content = strip_tags( do_shortcode( $post->post_content ) );
do_shortcode triggers all visual composer shortcodes and thus returns html+text;
strip_tags removes all html tags and returns plain text.
Here, you can try and easily add some short codes in array that you needs and also you can remove all shortcodes via below code.
$the_content = '[VC_ROW][VC_COLUMN]some text1[/VC_COLUMN] etc.[/VC_ROW][VC_COLUMN_INNTER width="1/3"][/VC_COLUMN_INNTER]';
$shortcode_tags = array('VC_COLUMN_INNTER');
$values = array_values( $shortcode_tags );
$exclude_codes = implode( '|', $values );
// strip all shortcodes but keep content
// $the_content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $the_content);
// strip all shortcodes except $exclude_codes and keep all content
$the_content = preg_replace( "~(?:\[/?)(?!(?:$exclude_codes))[^/\]]+/?\]~s", '', $the_content );
echo $the_content;
you want to remain some shortcodes you can't use strip_shortcodes() for that.
Best solution, solved.
Just add the following code to file wp-includes/rest-api.php, at the bottom:
/**
* Modify REST API content for pages to force
* shortcodes to render since Visual Composer does not
* do this
*/
add_action( 'rest_api_init', function ()
{
register_rest_field(
'page',
'content',
array(
'get_callback' => 'compasshb_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function compasshb_do_shortcodes( $object, $field_name, $request )
{
WPBMap::addAllMappedShortcodes(); // This does all the work
global $post;
$post = get_post ($object['id']);
$output['rendered'] = apply_filters( 'the_content', $post->post_content );
return $output;
}
I took it somewhere and update it a bit, to work a bit better :).
in functions.php add this function:
/** Function that cuts post excerpt to the number of a word based on previously set global * variable $word_count, which is defined below */
if(!function_exists('kc_excerpt')) {
function kc_excerpt($excerpt_length = 20) {
global $word_count, $post;
$word_count = $excerpt_length;
$post_excerpt = get_the_excerpt($post) != "" ? get_the_excerpt($post) : strip_tags(do_shortcode(get_the_content($post)));
$clean_excerpt = strpos($post_excerpt, '...') ? strstr($post_excerpt, '...', true) : $post_excerpt;
/** add by PR */
$clean_excerpt = strip_shortcodes(remove_vc_from_excerpt($clean_excerpt));
/** end PR mod */
$excerpt_word_array = explode (' ',$clean_excerpt);
$excerpt_word_array = array_slice ($excerpt_word_array, 0, $word_count);
$excerpt = implode (' ', $excerpt_word_array).'...'; echo ''.$excerpt.'';
}
}
and after that you call it normally kc_excerpt(20); and it will return normal post_content/excerpt
How to remove visual composer from wp post: i.e [vc_row][vc_column width=\"2/3\"][distance][vc_single_image image=\"40530\" img_size=\"large\"][distance][distance][distance][vc_column_text]
Also WP post remvoe short codes and html tags.
while($posts->have_posts()) {
$postContent = get_the_content();
//Remove html tags. and short code
$postContent = strip_tags( do_shortcode( $postContent ) );
//Remove visual composer tags [vc_column] etc
$postContent = preg_replace( "/\[(\/*)?vc_(.*?)\]/", '', $postContent );
}
Looking source code from wordpress api, I did a function to remove some shortcode from content. So this is the result:
function removeShortcode($content, $shortcodeList){
preg_match_all('#\[([^<>&/\[\]\x00-\x20=]++)#', $content, $matches);
$tagnames = array_intersect($shortcodeList, $matches[1]);
if(empty($tagnames)){
return $content;
}
$pattern = get_shortcode_regex($tagnames);
preg_match_all("/$pattern/", $content, $matchesTags);
foreach ($matchesTags[0] as $key => $value) {
$content = str_replace($value, $matchesTags[5][$key], $content);
}
return $content;
}
example:
$content = "<p>Hi, this is a [example]<b>example</b>[/example]. [end]</p>";
$shortcodesToRemove = ["example", "end"];
echo removeShortcode($content, $shortcodesToRemove);
foreach($posts as $po){
$services_array[] = array('id'=>$po->ID,'title'=>$po->post_title, 'description'=>do_shortcode($po->post_content));
}
You should try this.
I'm currently pulling in multiple pieces of content into my posts through an AFC with the value: "section_content". Furthermore, I'm using the code below to clean up my WP posts. How would I modify this filter to also include my ACF?
<?php
/**
* Clean posts from inline styling and unnecessary tags
*/
add_filter( 'the_content', 'clean_post_content' );
function clean_post_content($content) {
if ( is_single() ) {
$patterns = array(
'/(<[^>]+) style=".*?"/i', // Remove inline styling
'/<\/?font[^>]*>/', // Remove font tag
'/<(p|span)>(?>\s+| |(?R))*<\/\1>/', // Empty p, span (font tags already removed)
'/(<h[1-6]>[^<]*)<\/?strong>(.*?<\/h[1-6]>)/', // h1-6
);
$replacements = array(
'$1',
'',
'',
'$1$2'
);
$old_content = '';
while ($old_content != $content) {
$old_content = $content;
$content = preg_replace($patterns, $replacements, $content);
}
}
return $content;
}
?>
I guess you could use ACF Filters. I usually hook to acf/format_value in order to clean or modify my custom field values before printing them.
You can even hook to certain field types only, like:
function acf_brand_trademark( $value, $post_id, $field ) {
$value = preg_replace( '/Brand /', 'Brand<sup>™</sup> ', $value );
return $value;
}
add_filter('acf/format_value/type=textarea', 'acf_brand_trademark', 10, 3);
add_filter('acf/format_value/type=text', 'acf_brand_trademark', 10, 3);
Hope this helps!
There is a way to delete a specific shortcode, maintaining the text inside?
For example: in this case [dropcap]A[/dropcap] I would like to eliminate the shortcode maintaining the "A", or any other letter inside.
Thanks!
Use this JS regex to strip out any characters between square brackets from the text, while leaving any text that might've come beween shortcode tags, like [dropcap]A[/dropcap].
var myReg = /\[.+\]/g;
paragraphText = paragraphText.replace(myReg, '');
or to remove a shortcode like:
[media-credit param=value param2="value2"]text you actually want goes here[/media-credit]
You can use the following to your functions.php file:
add_filter( 'the_excerpt', 'remove_media_credit_from_excerpt' );
function remove_media_credit_from_excerpt( $excerpt ) {
return preg_replace ('/\[media-credit[^\]]*\](.*)\[\/media-credit\]/', '$1', $excerpt);
}
Here's a plugin that will launch one time and parse ALL POSTS' content, stripping the shortcodes (and leaving the content) of any desired shortcodes. Simply enter the shortcodes you want to strip in the $shortcode_tags array and the post type you want to perform on in the $posts array.
NOTE: This will impact your database and can NOT be undone. It is highly recommended that you backup your database first.
<?php
/*
Plugin Name: Strip Shortcodes Example
*/
add_action( 'init', '_replace_shortcodes' );
function _replace_shortcodes() {
global $shortcode_tags;
// Make sure this only happens ONE time
if ( get_option( '_replace_shortcodes_did_once' ) !== false ) {
return;
}
update_option( '_replace_shortcodes_did_once', true );
add_action( 'admin_notices', '_replace_shortcodes_notice' );
// Get all of our posts
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'post', // Change this for other post types (can be "any")
));
// Make WP think this is the only shortcode when getting the regex (put in all shortcodes to perform on here)
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array(
'dropcap' => null,
);
$shortcode_regex = get_shortcode_regex();
$shortcode_tags = $orig_shortcode_tags;
// Loop through the posts
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
// Perform Regex to strip shortcodes for given shortcodes
$post_content = preg_replace_callback( "/$shortcode_regex/s", '_replace_shortcodes_callback', $post->post_content );
// Update our post in the database
wp_update_post( array(
'ID' => $post->ID,
'post_content' => $post_content,
) );
}
}
}
function _replace_shortcodes_callback( $matches ) {
// This is the shortcode content
$content = $matches[5];
// No content, so just return the whole thing unmodified
if ( empty( $content ) ) {
return $matches[0];
}
// Otherwise, just return the content (with no shortcodes)
return $content;
}
function _replace_shortcodes_notice() {
?>
<div class="updated">
<p>
All posts' content updated.
</p>
</div>
<?php
}
function awepop_show_views($singular = "view", $plural = "views", $before = "This post has: ")
{
global $post;
$current_views = awepop_get_view_count();
$views_text = $before . $current_views . " ";
if ($current_views == 1) {
$views_text .= $singular;
}
else {
$views_text .= $plural;
}
return $views_text;
}
function awepop_append_to_meta($meta){
return $meta[] = awepop_show_views();
}
add_filter( 'the_meta_key', 'awepop_append_to_meta' );
I am trying to embed post views into post meta. I search alot but couldn't find the appropriate filter for post meta. please advice how can i embed my view count into post meta
Try hooking to init and using update_post_meta. For example, this is how you could increment your post views:
function my_update_postmeta() {
global $post;
update_post_meta( $post->ID, my_meta_key, $my_meta_value + 1 );
}
add_action( 'init', 'my_update_postmeta' );
Note: You would put my_update_postmeta() in your page or post template.
Ref: http://codex.wordpress.org/Function_Reference/update_post_meta