7

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.

SergeevDMS
  • 801
  • 6
  • 15

2 Answers2

12

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);
        }
Mikhail
  • 2,542
  • 4
  • 29
  • 40
2

In your dashboard go to Settings -> General and the fourth and fifth option should be "WordPress Address (URL)" and "Site Address (URL)". Change the http://example.com to http://www.example.com and it should change all links.

Howli
  • 12,291
  • 19
  • 47
  • 72
  • Thanks for your answer, but this solution is not for me. – SergeevDMS Feb 25 '14 at 16:36
  • Okay, can you explain why that isn't the solution? – Howli Feb 25 '14 at 16:48
  • Because on our hosting this options is remove from admin panel. – SergeevDMS Feb 26 '14 at 06:23
  • in 2016 this in functions.php results in a blank url `add_filter( 'post_link', array($this, 'changePermalinks'), 10, 3);function changePermalinks($permalink, $post) { $search = array('#devauthor#is','#author#is'); $replace = array('www','www'); $thePermalink = preg_replace($search, $replace, $permalink); return $thePermalink; }` – roberthuttinger Mar 25 '16 at 15:05
  • 1
    ^ if not in object context then shouldn't use `$this` keyword. So it'll be `add_filter( 'post_link' , 'changePermalinks')` as opposed to `add_filter( 'post_link' , array($this, 'changePermalinks'))` – Joe Buckle Jul 14 '17 at 10:35