Hello,
For a specific and important reason, I needed to add the full comment template of a specific post somewhere else (in another page, or in homepage). I succeed to do that. Now the problem is that when I want to publish a comment, it doesn't publish the comment on the good post. I understand why because when publishing a comment it usually "take" the "natural" post ID when it's on single.php
Is there a way somewhere in your plugin (code) where I could specify a post ID when posting a comment ? At least could you specify which of your file I should look at?
Thank you
Hi @mathieufd,
you should use the wpdiscuz_js_options hook, and change the value of the wc_post_id key (set it the current post ID ).
Great!
Where should I look at? (didn't find the hook)
you can find it in the wp-content/plugins/wpdiscuzclass.WpdiscuzCore.php file.
Alright, thank you.
Is there a way to clean the post_ID that have been taking in account so far (without refresh of course) ? I need that because I don't refresh the page when I change the post to be commented. Everything is loaded with ajax. Each time I load your template, I redefined wpdiscuzAjaxObj with the right Post_ID by loading one more time (screenshot) the var wpdiscuzAjaxObj (ajax).
So when I change wc_post_id dynamically, the correct post ID is displayed in wpdiscuzAjaxObj (var console gives me the good post ID). So if I open (ajax) the post form with a post ID #123, I will be able to comment for the post #123. But the problem is when I open a second form (ajax) without refreshing, I will be able to comment the seconde post but it will ALSO comment the first one and even if I redefine wpdiscuzAjaxObj, both posts will be commented.
I tried to redefine :
- var wpdiscuzAjaxObj
- var wpdiscuzPostId
- var postId
Hi @mathieufd,
I apologize for the late response.
Is there a way to clean the post_ID that have been taking in account so far (without refresh of course) ?
Yes, we've checked one more time and found that this cannot be reached yet. We'll take into consideration to change this in future version release.
Hi,
It's probably a bit late but I found a solution for this... basically I wanted to display wpdiscuz comments for photo posts in popups. My functionality exists in a plugin itself. Here's how I did it.
1. wpdiscuz uses the global $post->ID by default for comment functionality. This means the page id rather than photo id is being used when a comment within a photo popup is posted. So... firstly I needed to find a way to pass the photo id to the wpdiscuz plugin.
2. To do this I looked in wpdiscuz plugins directory > class.WpdiscuzCore.php. A wpdiscuz-ajax-js script is enqueued here and then the variable wpdiscuzAjaxObj is localised. This variable contains an object with the post_id as a property to be used for ajax operations. I specifically needed to update this variable on a click event of each call-to-action child within a comment wrapper element (I wrapped a <div> around each photo's comments area). On the comment wrapper I specified a data-pid attribute containing the photo id. Now the event handler on the child element can reference this when clicked, eg.
wpdiscuzAjaxObj.wpdiscuz_options.wc_post_id = $(this).closest('.comment-wrap').data('pid');
});
3. The enqueued wpdiscux-ajaz-js script (wpdiscuz.js) sets a local variable in an encapsulated jquery function to point to the value of the localised wpdiscuzAjaxObj variable (that I set on click), i.e.
var wpdiscuzPostId = wpdiscuzAjaxObj.wpdiscuz_options.wc_post_id;
This variable is out of reach from my script so I took a copy of this file in my plugin and customised it by commenting out the above assignment and replacing any usages of wpdiscuzPostId with the global variable wpdiscuzAjaxObj.wpdiscuz_options.wc_post_id.
global $wp_scripts;
$wpdiscuzOptionsJs = $wp_scripts->get_data('wpdiscuz-ajax-js', 'data');
if ( !is_array($wpdiscuzOptionsJs) ) {
$wpdiscuzOptionsJs = json_decode(str_replace('var wpdiscuzAjaxObj = ', '', substr($wpdiscuzOptionsJs, 0, -1)), true);
}
if ( is_page('xxx') ) {
// Slight modified copy of wpdiscuz needed for this page to accept passed in/popup post ids, rather than using the default post id.
wp_dequeue_script( 'wpdiscuz-ajax-js' );
wp_enqueue_script( 'custom-wpdiscuz-ajax-js', PHOTO_PLUGIN_URL . 'stream/js/wpdiscuz.js', array('jquery'), 'custom.5.3.5', true );
wp_localize_script('custom-wpdiscuz-ajax-js', 'wpdiscuzAjaxObj', array('url' => admin_url('admin-ajax.php'), 'customAjaxUrl' => plugins_url() . '/wpdiscuz/utils/ajax/wpdiscuz-ajax.php', 'wpdiscuz_options' => $wpdiscuzOptionsJs));
}
I tried to edit my previous post but something went wrong, small changes to the enqueue script (better naming and a slight simplification):
global $wp_scripts;
$wpdiscuzAjaxData = $wp_scripts->get_data('wpdiscuz-ajax-js', 'data');
if ( !is_array($wpdiscuzAjaxData) ) {
$wpdiscuzAjaxData = json_decode(str_replace('var wpdiscuzAjaxObj = ', '', substr($wpdiscuzAjaxData, 0, -1)), true);
}
if ( is_page('xxx') ) {
// Slight modified copy of wpdiscuz needed for this page to accept passed in/popup post ids, rather than using the default post id.
wp_dequeue_script( 'wpdiscuz-ajax-js' );
wp_enqueue_script( 'custom-wpdiscuz-ajax-js', MOP_PHOTO_PLUGIN_URL . 'stream/js/wpdiscuz.js', array('jquery'), 'custom.5.3.5', true );
wp_localize_script( 'custom-wpdiscuz-ajax-js', 'wpdiscuzAjaxObj', $wpdiscuzAjaxData );
}