Skip to main content
deleted 164 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

Here's the scenario: I wrote a custom url rule to pass a comment id to a template and display the comment and comment meta as a single page (eg. http://mydomain.com/reply/56eg. http://example.com/reply/56) and it works fine. But now I'm trying to refine this rule to include the comment'scomments post slug in the urlURL. So when I visit 'http://mydomain.com/reply/56''http://example.com/reply/56' it should map to 'http://mydomain.com/the-post/reply/56''http://example.com/the-post/reply/56'. I'm actually stuck at this point. Here's what I've done till now: The rule that works:

<?php 
add_rewrite_rule( '^reply/(.*)?$', 'index.php?pagename=reply-page&reply_id=$matches[1]', 'top' );
?>

Added a query_var 'reply_id'query_var 'reply_id' for this.

Now the code I'm trying:

<?php
add_action('init', 'test');
function test()
{
    $reply_struct = '/reply/%reply_id%';
    $wp_rewrite->add_rewrite_tag('%reply_id%', '([^/]+)', 'reply_id=$matches[1]');
    $wp_rewrite->add_permastruct('reply_id', $reply_struct, false);
}

add_filter('post_type_link', 'reply_permalink', 10, 3)
function reply_permalink()
{
    $rewritecode= array(
        '%reply_id%'
    );

    $test = '';
    if ( strpos($permalink, '%reply_id%') !== false ) 
    {
        $i = get_query_var('reply_id'); //Trying to get the comment id, this is where I'll get the post slug and append it to the url
        $test = 'test';
     }

     $rewritereplace = array(
       $test
     );
     $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
     return $permalink;
}

I think when I access the url 'http://mydomain.com/reply/56'URL 'http://example.com/reply/56', I should get 'http://mydomain.com/test/''http://example.com/test/' or 'http://mydomain.com/reply/test''http://example.com/reply/test'. But I'm not! I know I'm doing it wrong somewhere, but don't know where. I must say, url rewrite is one topic in WP that always stumps me! Please let me know if there is another way to do this!

Here's the scenario: I wrote a custom url rule to pass a comment id to a template and display the comment and comment meta as a single page (eg. http://mydomain.com/reply/56) and it works fine. But now I'm trying to refine this rule to include the comment's post slug in the url. So when I visit 'http://mydomain.com/reply/56' it should map to 'http://mydomain.com/the-post/reply/56'. I'm actually stuck at this point. Here's what I've done till now: The rule that works:

<?php 
add_rewrite_rule('^reply/(.*)?$','index.php?pagename=reply-page&reply_id=$matches[1]','top');
?>

Added a query_var 'reply_id' for this.

Now the code I'm trying:

<?php
add_action('init', 'test');
function test()
{
    $reply_struct = '/reply/%reply_id%';
    $wp_rewrite->add_rewrite_tag('%reply_id%', '([^/]+)', 'reply_id=$matches[1]');
    $wp_rewrite->add_permastruct('reply_id', $reply_struct, false);
}

add_filter('post_type_link', 'reply_permalink', 10, 3)
function reply_permalink()
{
    $rewritecode= array(
        '%reply_id%'
    );

    $test = '';
    if ( strpos($permalink, '%reply_id%') !== false ) 
    {
        $i = get_query_var('reply_id'); //Trying to get the comment id, this is where I'll get the post slug and append it to the url
        $test = 'test';
     }

     $rewritereplace = array(
       $test
     );
     $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
     return $permalink;
}

I think when I access the url 'http://mydomain.com/reply/56', I should get 'http://mydomain.com/test/' or 'http://mydomain.com/reply/test'. But I'm not! I know I'm doing it wrong somewhere, but don't know where. I must say, url rewrite is one topic in WP that always stumps me! Please let me know if there is another way to do this!

Here's the scenario: I wrote a custom url rule to pass a comment id to a template and display the comment and comment meta as a single page (eg. http://example.com/reply/56) and it works fine. But now I'm trying to refine this rule to include the comments post slug in the URL. So when I visit 'http://example.com/reply/56' it should map to 'http://example.com/the-post/reply/56'. I'm actually stuck at this point. Here's what I've done till now: The rule that works:

<?php 
add_rewrite_rule( '^reply/(.*)?$', 'index.php?pagename=reply-page&reply_id=$matches[1]', 'top' );
?>

Added a query_var 'reply_id' for this.

Now the code I'm trying:

<?php
add_action('init', 'test');
function test()
{
    $reply_struct = '/reply/%reply_id%';
    $wp_rewrite->add_rewrite_tag('%reply_id%', '([^/]+)', 'reply_id=$matches[1]');
    $wp_rewrite->add_permastruct('reply_id', $reply_struct, false);
}

add_filter('post_type_link', 'reply_permalink', 10, 3)
function reply_permalink()
{
    $rewritecode= array(
        '%reply_id%'
    );

    $test = '';
    if ( strpos($permalink, '%reply_id%') !== false ) 
    {
        $i = get_query_var('reply_id'); //Trying to get the comment id, this is where I'll get the post slug and append it to the url
        $test = 'test';
     }

     $rewritereplace = array(
       $test
     );
     $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
     return $permalink;
}

I think when I access the URL 'http://example.com/reply/56', I should get 'http://example.com/test/' or 'http://example.com/reply/test'. But I'm not.

Source Link
Rutwick Gangurde
  • 8.6k
  • 5
  • 42
  • 55

Pass a comment id through url and append the comment post's slug in the url

Here's the scenario: I wrote a custom url rule to pass a comment id to a template and display the comment and comment meta as a single page (eg. http://mydomain.com/reply/56) and it works fine. But now I'm trying to refine this rule to include the comment's post slug in the url. So when I visit 'http://mydomain.com/reply/56' it should map to 'http://mydomain.com/the-post/reply/56'. I'm actually stuck at this point. Here's what I've done till now: The rule that works:

<?php 
add_rewrite_rule('^reply/(.*)?$','index.php?pagename=reply-page&reply_id=$matches[1]','top');
?>

Added a query_var 'reply_id' for this.

Now the code I'm trying:

<?php
add_action('init', 'test');
function test()
{
    $reply_struct = '/reply/%reply_id%';
    $wp_rewrite->add_rewrite_tag('%reply_id%', '([^/]+)', 'reply_id=$matches[1]');
    $wp_rewrite->add_permastruct('reply_id', $reply_struct, false);
}

add_filter('post_type_link', 'reply_permalink', 10, 3)
function reply_permalink()
{
    $rewritecode= array(
        '%reply_id%'
    );

    $test = '';
    if ( strpos($permalink, '%reply_id%') !== false ) 
    {
        $i = get_query_var('reply_id'); //Trying to get the comment id, this is where I'll get the post slug and append it to the url
        $test = 'test';
     }

     $rewritereplace = array(
       $test
     );
     $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
     return $permalink;
}

I think when I access the url 'http://mydomain.com/reply/56', I should get 'http://mydomain.com/test/' or 'http://mydomain.com/reply/test'. But I'm not! I know I'm doing it wrong somewhere, but don't know where. I must say, url rewrite is one topic in WP that always stumps me! Please let me know if there is another way to do this!