wp_editor content doesn't show in theme option - php

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

Related

Creating a wordpress virtual page with a function and page template

I'm trying to create a virtual contact page on wordpress. All the necessary data will be stored in a page template. But in my function I don't know how to link to the file.
When the user does to domain.com/contact I would like to redirect him to the contact.php file.
So far I got this function to work and show content from this line $post->post_content = 'page template: contact.php'; but I would like to show the content from templates/contact.php
add_filter( 'the_posts', 'generate_contact_page', -10 );
function generate_contact_page( $posts ) {
global $wp, $wp_query;
$url_slug = 'contact'; // URL slug of the contact page
if ( ! defined( 'CONTACT_PAGE' ) && ( strtolower( $wp->request ) == $url_slug ) ) {
// stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
define( 'CONTACT_PAGE', true );
// create a contact virtual page
$post = new stdClass;
$post->post_author = 1;
$post->post_name = $url_slug;
$post->guid = home_url() . '/' . $url_slug;
$post->post_title = 'Contact page';
$post->post_content = 'page template: contact.php';
$post->ID = -999;
$post->post_type = 'page';
$post->post_status = 'static';
$post->comment_status = 'closed';
$post->ping_status = 'open';
$post->comment_count = 0;
$post->post_date = current_time( 'mysql' );
$post->post_date_gmt = current_time( 'mysql', 1 );
$posts = NULL;
$posts[] = $post;
// make wpQuery believe this is a real page too
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
unset( $wp_query->query[ 'error' ] );
$wp_query->query_vars[ 'error' ] = '';
$wp_query->is_404 = false;
}
return $posts;
}
How can I achieve something like this? Maybe you know a better solution ?
So for template output you can try this function
function get_template_output($slug = '', $name = '') {
ob_start();
get_template_part($lug, $name);
return ob_get_clean();
}
And then to assign content you will use
$content = get_template_output('templates/', 'contact');

Update all posts meta at the same time in Wordpress

I want to run a function that will update all posts. My problem is that the function only runs when I visit that specific post (Only the specific post will be updated).
My function updates Facebook likes from the post source URL
function update_facebook_likes($content) {
global $post;
$url_path = get_post_meta(get_the_ID(), 'url_source', TRUE);
$data = file_get_contents('https://graph.facebook.com/v2.2/?id='.$url_path.'&fields=share&access_token=****');
$obj = json_decode($data);
$like_na = $obj->{'share'};
$like_no = $like_na->{'share_count'};
update_post_meta($post->ID, 'fb_likes_count', $like_no);
}
add_action('wp', 'update_facebook_likes');
function display_fb_likes() {
global $post;
$fb_likes_count = get_post_meta($post->ID, 'fb_likes_count', true);
echo $fb_likes_count;
}
Try to use this foreach loop to update all posts. using get_posts() function
https://developer.wordpress.org/reference/functions/get_posts/
$args = array(
'posts_per_page' => -1,
);
$posts_array = get_posts( $args );
foreach($posts_array as $post_array)
{
update_post_meta($post_array->ID, 'fb_likes_count', true);
}

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/

Render wp_editor input in theme file using get_post_meta

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;

Run function on posts sequentially

I have the following (resource intensive) function that I am triggering on the save_post action. It works perfectly when I save OR edit a single post. The problem is that I need to use it when importing multiple posts at the same time. I believe it is running this function across ALL imported (saved) posts at the same time.
How can I use the save_post action BUT only have this function execute on each post sequentially?
function getYouTubeTags( $post_id ) {
// Terminate if post has any tags
if ( has_term('', 'post_tag') ) {
return;
}
$out = null;
$video_id = get_post_meta( get_the_ID(), 'rfvi_video_id', true );
$tag_url = "http://www.youtube.com/watch?v=" . $video_id;
$sites_html = file_get_contents($tag_url, null, null, 700, 7000);
$html = new DOMDocument();
#$html->loadHTML($sites_html);
$meta_og_tag = null;
foreach( $html->getElementsByTagName('meta') as $meta ) {
if( $meta->getAttribute('property')==='og:video:tag' ){
$meta_og_tag = $meta->getAttribute('content');
$out[] = $meta_og_tag;
}
}
if ( !empty($out) ) {
return implode(',', $out);
} else {
return;
}
}
// Add YouTube tags to post
function rct_save_post_terms( $post_id ) {
$terms = getYouTubeTags( $post_id );
wp_set_post_terms( $post_id, $terms, 'post_tag', true );
}
add_action( 'save_post', 'rct_save_post_terms', 110, 1 );

Categories