Render wp_editor input in theme file using get_post_meta - php

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;

Related

How to inspect variables inside "register_activation_hook" in WordPress

I'm new to WordPress/PHP and I'm trying to create a WP plugin. I want to be able to inspect some code inside the register_activation_hook. This is the code that's being executed inside the register_activation_hook:
$data_store = \WC_Data_Store::load('webhook');
$all_hooks = $data_store->search_webhooks(
array(
'limit' => 10,
)
);
$_items = array_map('wc_get_webhook', $all_hooks->webhooks);
foreach ($_items as $webhook) {
$name = $webhook->get_name();
echo $name; // trying to inspect the name here
if ($name === "Test Hook") {
$webhook->set_status('active');
$webhook->save();
}
}
When I install and then activate the plugin, I don't get any output anywhere, even if I use alert instead of echo.
I saw some people saying that you shouldn't output anything inside the register_activation_hook. If that's the case, how can we inspect the values inside this hook?
I tried using a debugging plugin called Query Monitor, but it's also not capturing anything.
You can't pass output to the register_activation_hooksince it will return a Headers already sent error, you should catch the values you need with a transient and pass it to the admin_notices action, similar to this:
register_activation_hook( __FILE__, 'initiate_webhooks' );
function initiate_webhooks(){
$data_store = \WC_Data_Store::load( 'webhook' );
$all_hooks = $data_store->search_webhooks(
array(
'limit' => 10,
)
);
$_items = array_map( 'wc_get_webhook', $all_hooks->webhooks );
set_transient( 'active_webhooks', wp_json_encode( $_items ) , 60 );
foreach ( $_items as $webhook ) {
$name = $webhook->get_name();
if ( $name === "Test Hook" ) {
$webhook->set_status( 'active' );
$webhook->save();
}
}
}
add_action( 'admin_notices', function() {
$webhooks = get_transient( 'active_webhooks' );
if ( false !== $webhooks ) {
?>
<div class="notice notice-success is-dismissible">
<?php
$webhooks = json_decode( $webhooks );
foreach( $webhooks as $webhook ) {
echo $webhook;
}
?>
</div>
<?php
}
});
Don't forget to delete the transient.
https://developer.wordpress.org/apis/transients/

Extract shortcode content from another post

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' );

Wordpress - send variable from page to theme function

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/

Shortcode output always showing at top of custom template

my shortcode output always appear on the top of my custom template.
custom template
$tag= 'the_content';
remove_all_filters( $tag);
$postid = get_the_ID();
$post = get_post($postid);
$content = do_shortcode($post->post_content);
ob_start();
echo $content;
$result = ob_get_contents();
ob_end_clean();
return $result;
custom shortcode
function signupform_shortcode( $atts ) {
extract( shortcode_atts( array(
'socialmkt' => 'aweber'
), $atts ) );
if($socialmkt == 'aweber'){
if($display == 'popup') {
return include_once('modal-aweber.php');
}
}
}
add_shortcode('signupform', 'signupform_shortcode');
The shortcode it's place in the middle of a html landing.
I try adding ob_start() that i read in other posts but still not working.
Your shortcode callback is going to output content rather than return it which is why you're seeing it appear at the top of your page.
Using output buffering (ob_start() / ob_get_contents()) is a valid way of addressing the problem however you need to move the code.
Output buffering should be taking place inside your shortcode callback where the return value is required instead of output.
function signupform_shortcode( $atts ) {
extract( shortcode_atts( array(
'socialmkt' => 'aweber'
), $atts ) );
// Begin output buffering here. Any output below will be stored in a buffer.
ob_start();
if ( $socialmkt == 'aweber' ) {
if ( $display == 'popup' ) {
// Return, as previously used, doesn't help in this context.
include_once( 'modal-aweber.php' );
}
}
// Return (and delete unlike ob_get_contents()) the content of the buffer.
return ob_get_clean();
}
add_shortcode( 'signupform', 'signupform_shortcode' );

wp_editor content doesn't show in theme option

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() );
}

Categories