I have a custom page in Wordpress with a custom template. In addition to showing users the actual page, it also dynamically creates pages via passed in URL variables.
So I created a page called News in wordpress and assigned it to my news.php template. Users can hit the page at mywebsite.com/news
They can also goto mywebsite.com/news/2012/august-8/ and the page template reads in the date via these url variables and it shows news for just that page.
Here's what I want to do. I want to add comments to the "date specific pages" which are not actual pages inside wordpress, but are created on the fly based on url variables.
I can add comments_template() to the page, but i believe it is based on page id or post id... is there a way to insert a custom id or insert the url to create comments for these dynamic pages?
I don't want the comments on mywebsite.com/news/2012/august-8/ to show up on mywebsite.com/news/2012/august-9/ --- they are separate pages
thoughts?
I understand you but I think is not good idea use WP in such way. You are messing the main functionality of WP, their posts and comments. I should use another framework for this job. Any way a good starting point to investigate is wp_commentmeta
table and their friends:
add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false)
get_comment_meta( $comment_id, $meta_key, $single = false )
Sure that you can achieve what you need.
I know this question is old, but I'm going to post the answer anyway in-case people are still looking. Honestly I think this feature should be included with plugins that create dynamic user profile type pages and I'm not sure why developers have added this feature in.
As long as their is part of the url that is unique in the dynamic pages this method will work. In my case I use the second part of the url.
First you want to add an extra hidden comment field to your comments that grabs part or all of the url and populates it in the text box. You can either do this to all the comments on your website or you can use an if query like I did if you only need it on one parent page.
Next you can make sure this textbox is posted to your wp_commentmeta table as extra comment meta data. Now you have a unique id for each comment posted to a specific url that you can query the database for.
// adds the extra comment field and populates it with the second part
// of the the url for example (mydomain.com/profile/userid -> $lastpart[2] = userid)
// also posts data to database. the ability to edit is there in case you want to show the element
// this part would go in your functions.php
function unique_comments_additional_field() {
global $post_id;
if(is_page(108)){
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$path = parse_url($url, PHP_URL_PATH);
$lastpart = explode('/', rtrim($path, '/'));
echo '<p style="display:none;">'.
'<input id="hiddencoment" name="hiddencoment" placeholder="' . __( 'hiddencoment', 'Extra Field' ) . '" value="'.$lastpart[2].'" type="text" size="30" /></p>';
}
add_action( 'comment_form_logged_in_after', 'unique_comments_additional_field' );
add_action( 'comment_form_after_fields', 'unique_comments_additional_field' );
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['hiddencomment'] ) ) && ( $_POST['hiddencomment'] != '') ) {
$hiddencomment = wp_filter_nohtml_kses($_POST['hiddencomment']);
add_comment_meta( $comment_id, 'hiddencomment', $hiddencomment );
}
}
add_action( 'comment_post', 'save_comment_meta_data' );
function unique_extend_comment_add_meta_box() {
add_meta_box( 'hiddencomment', __( 'Comment Metadata', 'Extra Field' ), 'unique_extend_comment_meta_box', 'comment', 'normal', 'high' );
}
add_action( 'add_meta_boxes_comment', 'unique_extend_comment_add_meta_box' );
function unique_extend_comment_meta_box( $comment ) {
$hiddencomment = get_comment_meta( $comment->comment_ID, 'hiddencomment', true );
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
?>
<p>
<input type="text" style="display:none;" name="hiddencomment" value="<?php echo esc_attr( $hiddencomment ); ?>" class="widefat" />
</p>
<?php
}
function unique_extend_comment_edit_metafields( $comment_id ) {
if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;
if ( ( isset( $_POST['hiddencomment'] ) ) && ( $_POST['hiddencomment'] != '') ):
$hiddencomment = wp_filter_nohtml_kses($_POST['hiddencomment']);
update_comment_meta( $comment_id, 'hiddencomment', $hiddencomment);
else :
delete_comment_meta( $comment_id, 'hiddencomment');
endif;
}
add_action( 'edit_comment', 'unique_extend_comment_edit_metafields' );
Query to show comments that have a meta value that match the part of the url we just saved to the commentmeta table. This would go in your parent post page template i.e. in this example page 108
<ol class="commentlist" style="list-style: none; background: #eee; padding: 10px;">
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$path = parse_url($url, PHP_URL_PATH);
$lastpart = explode('/', rtrim($path, '/'));
$customer_id = (string)$lastpart[2];
//Gather comments for a specific page/post
$comments = get_comments(array( 'meta_key' => 'title', 'meta_value' => $customer_id ) );
//Display the list of comments
wp_list_comments(array(
'per_page' => 10, //Allow comment pagination
'reverse_top_level' => true //Show the latest comments at the top of the list
), $comments);
?>
</ol>