edonbajrami
edonbajrami

Reputation: 2206

WordPress cannot modify header

I have a website with WordPress 4.6 installed, trying to move on menu item click open first children page. Trying to do that wp_redirect but it is not working.

Warning: Cannot modify header information - headers already sent by (output started at /home/content/65/9303265/html/wp-content/themes/ThemeName/page-gotochild.php:8) in /home/content/65/9303265/html/wp-includes/pluggable.php on line 1174

I have tried to remove blank spaces and lines and replace wp-admin and wp-includes but it is not working.

Code that I am using to redirect:

<?php
/*
    Template Name: Go to first child
*/
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pagekids) {
    $firstchild = $pagekids[0];
    wp_redirect(get_permalink($firstchild->ID));
}?>

Upvotes: 0

Views: 122

Answers (2)

edonbajrami
edonbajrami

Reputation: 2206

I have found my answer, on the root folder I had the file with same filename like the link that I want to redirect.

Upvotes: 0

Nicholas Koskowski
Nicholas Koskowski

Reputation: 849

<?php
/*
    Template Name: Go to first child
*/
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pagekids) {
   $firstchild = $pagekids[0];
   wp_redirect(get_permalink($firstchild->ID));
   exit;
}?>

Always try to call exit when doing a redirect. See this link in the codex for more information.

"Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;:"

Upvotes: 1

Related Questions