change URL for wordpress wp_get_archives function

I’m using the wp_get_archives function in WordPress.

<?php $args = array(
    'type'            => 'monthly',
    'limit'           => '',
    'format'          => 'html', 
    'before'          => '',
    'after'           => '',
    'show_post_count' => false,
    'echo'            => 1,
    'order'           => 'DESC'
); ?>

<?php wp_get_archives( $args ); ?> 

But I want to be able to change the URL – rather than it going to my WordPress installation directory, how can I change it to my own custom URL?

Read More

PS: I found on the general-template.php file starting at line 937 is where this function starts.

Related posts

Leave a Reply

2 comments

  1. I saw that, inside the function wp_get_archives, the links are built with the function get_archives_link(). There, we find the filter hook get_archives_link that returns each HTML link for the archives.

    So, we can manipulate each of this strings and replace stuff:

    add_filter( 'get_archives_link', function( $html ) 
    {
        if( is_admin() ) // Just in case, don't run on admin side
            return $html;
    
        // $html is '<li><a href='http://example.com/hello-world'>Hello world!</a></li>'
        $html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );
        return $html;
    });
    
  2. brasofilo’s solution worked for me, with some minor adjustments.

    before:

    $html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );
    

    after:

    $html = str_replace( 'http://example.com', 'http://example.com/the_events', $html );