Search code examples
wordpresspermalinks

General wordpress link filter


everyone! I need to add the domain prefix www, not to write by hand, each filter as post_link, page_link, category_link and so on - there is a global filter to all urls added www. Methods of how to change the general settings in the site url in the database or change options or htaccess - just do not fit. Thanks in advance for your reply.


Solution

  • If you can't change it via the wp-admin, you can use the following:

            add_filter( 'post_link', array($this, 'changePermalinks'), 10, 3);
            add_filter( 'page_link', array($this, 'changePermalinks'), 10, 3);
            add_filter( 'post_type_link', array($this, 'changePermalinks'), 10, 3);
            add_filter( 'category_link', array($this, 'changePermalinks'), 11, 3);
            add_filter( 'tag_link', array($this, 'changePermalinks'), 10, 3);
            add_filter( 'author_link', array($this, 'changePermalinks'), 11, 3);
            add_filter( 'day_link', array($this, 'changePermalinks'), 11, 3);
            add_filter( 'month_link', array($this, 'changePermalinks'), 11, 3);
            add_filter( 'year_link', array($this, 'changePermalinks'), 11, 3);
    
            function changePermalinks($permalink, $post) {
    
                    if ( strpos($permalink, '://www.') ) return $permalink;
    
                    return str_replace('://', '://www.', $permalink);
            }