wpDiscuz - WordPress Comment Plugin
Share:
Notifications
Clear all

How to enable wpDiscuz for Metabox content added with Ajax  


ComlyW
Posts: 2
(@comlyw)
New Member
Joined: 5 months ago

I have updated my backend so that when creating posts, there's an extra box to add content that is revealed (via ajax) when a button is clicked. 

I have installed wpDiscuz and it seems to allow me to highlight text and create shortcodes for content in the extra box. However, when the html is added through the ajax call, it appears as text instead of acting as a short code (no real surprise here).

My question here is how can I get the wpDiscuz shortcode to fire when this html is added to the DOM? Here is the relevant code of the extra box and the ajax call:

functions.php

add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_enqueue_script(
'extra-script', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . '/js/script.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
wp_localize_script( 'extra-script', 'my_ajaxurl', admin_url( 'admin-ajax.php' ) );
wp_localize_script('extra-script', 'my_postid', get_the_ID());
wp_localize_script('extra-script', 'nonce', array( 'nonce' => wp_create_nonce('ajax-nonce') ) );
}

add_action('wp_ajax_process_shortcode_on_click_action', 'process_shortcode_on_click_action');
add_action('wp_ajax_nopriv_process_shortcode_on_click_action', 'process_shortcode_on_click_action');

function process_shortcode_on_click_action() {
// you should check for a nonce and do other validation here to make sure this is a legit request

$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

if (empty($post_id = $_POST['post_id']) || !is_numeric($post_id)) {
wp_die('Post ID is Invalid', 400);
}

echo do_shortcode("<hr>" . "[extra post_id='{$post_id}']");
wp_die();
}

add_shortcode( 'extra', 't5_extra_content' );
add_action( 'add_meta_boxes_post', 't5_register_extra_metabox' );
add_action( 'save_post', 't5_save_shortcode_box', 10, 2);

function t5_extra_content( $attributes, $content = '' )
{
$defaults = [
'post_id' => get_the_ID(),
'cap' => 'edit_posts'
];

$args = shortcode_atts($defaults, $attributes);

if (!current_user_can($args['cap']) || empty($args['post_id'])) {
return ''; // or some message on fail
}

return wpautop(get_post_meta($args['post_id'], '_t5_extra_box', TRUE) . $content);
}

function t5_register_extra_metabox()
{
add_meta_box(
't5_extra',
'My Point of View',
't5_extra_metabox_callback',
NULL, // screen
'normal',
'default'
);
}
function t5_extra_metabox_callback( $post )
{
$nonce = wp_create_nonce( __FILE__ );
echo "<input type='hidden' name='t5_extra_box_nonce' value='$nonce' />";
$content = get_post_meta($post->ID, '_t5_extra_box', TRUE );
wp_editor(
$content,
'_t5_extra_box',
array (
'textarea_rows' => 15,
'media_buttons' => FALSE,
'teeny' => TRUE,
'tinymce' => TRUE
)
);
}
function t5_save_shortcode_box( $post_id )
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
or ! isset ( $_POST['post_type'] )
or 'post' !== $_POST['post_type']
or ! current_user_can( 'edit_post', $post_id )
or ! wp_verify_nonce( $_POST[ 't5_extra_box_nonce' ], __FILE__ )
)
{
return;
}

if ( isset ( $_POST['_t5_extra_box'] ) )
update_post_meta( $post_id, '_t5_extra_box', $_POST['_t5_extra_box'] );
else
delete_post_meta( $post_id, '_t5_extra_box' );
}

Ajax

jQuery(document).ready(function($) {
$('#extra').on('click',function() {
$.ajax({
type: "POST",
url: my_ajaxurl,
data: {
action : 'process_shortcode_on_click_action',
post_id: my_postid,
},
success:function(data) {
$('#extra-container').html(data);
},
error: function(errorThrown){
console.log("Error");
}
});
})
})

 

Topic Tags
3 Replies
Asti
Posts: 1669
 Asti
Support
(@asti)
Support member
Joined: 3 years ago

Hi@comlyw,

Please try to Enable "WordPress Shortcodes in Comment Content" option. The option is located in the Dashboard > wpDiscuz > Settings > Comment Content and Media admin page

Reply
ComlyW
Posts: 2
(@comlyw)
New Member
Joined: 5 months ago

Thank you @Asti, but unfortunately that didn't work. Any other suggestions?

Reply
1 Reply
Asti
 Asti
Support
(@asti)
Joined: 3 years ago

Support member
Posts: 1669

@comlyw,

Please send the admin login details to info[at]gvectors.com email address. I'll ask the developers to check the issue for you. 

Also, include some examples in the email content. 

Reply
Share: