I have a function in the theme function file that pulls JSON data and creates a shortcode that I use in a page
My question is how to pass the RID=Value from a page using the shortcode
[json_employees] and send the RID back to the function?
function foobar2_func( $atts ){
ob_start();
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965';
$data = file_get_contents($url);
$items = str_replace('<p></p>', '', $items);
$items = json_decode($data, true);
?>
add_shortcode( 'json_employees', 'foobar2_func' );
your shortcode should be
[json_employees RID='17965']
and shortcode function is
function foobar2_func( $atts ){
ob_start();
$atts = shortcode_atts(
array(
'RID' => 'id',
), $atts );
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']);
$data = file_get_contents($url);
$items = str_replace('<p></p>', '', $items);
$items = json_decode($data, true);
return $items;
}
add_shortcode( 'json_employees', 'foobar2_func' );
function foobar2_func( $atts ){
ob_start();
$atts = shortcode_atts(
array(
'RID' => 'id',
), $atts );
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']);
$data = file_get_contents($url);
$items = str_replace('<p></p>', '', $items);
$items = json_decode($data, true);
return $items;
print_r ($atts);
}
add_shortcode( 'json_employees', 'foobar2_func' );
and in the wordpress page
[json_employees RID='17965']
here is the page to test it http://bahiacr.com/test_json/
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 try to insert wp_editor. Everything is fine but the content doesn't show up after save content.
function display_slogan_element() {
$editor_id = "home_slogan";
$editor_class = "slogan";
$textarea_name = "slogan";
$content = get_post_meta( $post->ID, 'slogan', true);
$settings = array('teeny'=> TRUE);
wp_editor( $content, $editor_id, $settings = array() );
}
I'm not sure what is the problem. Anyone can help me ?
Can you please define global $post; after check it?
For example :
function display_slogan_element() {
global $post;
$editor_id = "home_slogan";
$editor_class = "slogan";
$textarea_name = "slogan";
$content = get_post_meta( $post->ID, 'slogan', true);
$settings = array('teeny'=> TRUE);
wp_editor( $content, $editor_id, $settings = array() );
}
I added a custom php function to my wordpress template, where I want to echo the content of pages under a certain parent:
Departments
-department 1 (get the title and the content clean)
-department 2 (get the title and the content clean)
(...)
The code I have so far is not working as i want, the title is fine but I need to filter the content so I can grab only the text between the "<p>" tag. Is this possible? Thank you.
functions.php
function echo_childs_of( $postID ) {
$args = array(
'order' => 'ASC',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'any'
);
$page_childs = get_children( $args );
if ( $page_childs ) {
foreach ( $page_childs as $child ) {
$title = get_the_title( $child );
$content = get_the_content($child);
$content = strip_shortcodes( $content );
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
echo $title;
echo $content;
}
}
}
on my php page
echo_childs_of( 7 );
You could use DOM to find the p tag elements and itarate over them using a for cycle to do whatever it is that you want to do.
$content = get_the_content($child);
$doc = new DOMDocument();
#$doc->loadHTML($content);
$p_elements = $doc->getElementsByTagName('p');
foreach ($p_elements as $p) {
//Do something with the p element... Strip tags maybe?
}
Try this:
$content = get_the_content($child);
preg_match('/<p>(.*)<\/p>/', $content, $match);
$content = $match[1];
I have a meta box with wp_editor other than the main content editor.
public function add_custom_meta_boxes() {
add_meta_box(
'ux_page_header',
__( 'Page Header', 'ux' ),
array($this, 'ux_render_page_header'),
'page',
'normal',
'core'
);
}
public function ux_render_page_header() {
$page_header = get_post_meta($_GET['post'], 'page_header' , true ) ;
wp_editor(
htmlspecialchars_decode($page_header),
'ux_page_header',
$settings = array('textarea_name'=>'page_header')
);
}
public function ux_save_post_data($post_id) {
if( !empty($_POST['page_header']) ) {
$data = htmlspecialchars($_POST['page_header']);
update_post_meta($post_id, 'page_header', $data );
}
}
But when I am retrieving this data, it just prints the html code. How to render this html?
$header = get_post_meta( get_the_ID(), 'page_header', true );
echo $header;
When you save the info here: $data = htmlspecialchars($_POST['page_header']); the data is encoded using htmlspecialchars so to render the content as HTML when calling get_post_meta, you will need to decode it:
$header = htmlspecialchars_decode(get_post_meta( get_the_ID(), 'page_header', true ));
echo $header;