Hot answers tagged

16

Found the answer (via) Instead of using the function I added an action to "wp_loaded", that makes sure that it gets loaded before any headers are sended. <?php add_action ('wp_loaded', 'my_custom_redirect'); function my_custom_redirect() { if ( isset( $_POST['subscribe'] ) ) { $redirect = 'http://example.com/redirect-example-url.html'; ...


15

Here is the code I've used, based on the original question and on Dominic P's answer... /* * Modify HTTP header */ function add_header_xua($headers) { // var_dump($headers); #=> if you want to see the current headers... if (!is_admin()) { $headers['X-UA-Compatible'] = 'IE=edge,chrome=1'; } return $headers; } ...


14

The output is generated by the rest_output_link_header(). This function is used in two actions, wp_head and template_redirect in default-filters.php:@line234. You can remove the function from those hooks to remove the output you wanted to remove. Put the following codes in your theme's functions.php to achieve the desired result. remove_action( 'wp_head', '...


13

Milo is correct. For instance, go to your theme's functions.php file, and add the following: add_filter( 'allowed_http_origins', 'add_allowed_origins' ); function add_allowed_origins( $origins ) { $origins[] = 'https://site1.example.com'; $origins[] = 'https://site2.example.com'; return $origins; } Now an ajax call from https://site1.example....


11

I know it's been a while, but if anyone else stumbles on this, I found a WordPress hook specifically for modifying HTTP headers. The hook is wp_headers and it's called in the wp class. The first argument passed is an array of headers with the header name as the key. The second argument is a reference to the wp class object.


11

get_header( $name ) is a Wordpress function, that will try to load the file header-{$name}.php from your theme's root folder. If this file doesn't exist, Wordpress will load the default header.php file.


11

Looking at wp-includes/template-loader.php … there seems to be a way: if ( $template = apply_filters( 'template_include', $template ) ) include( $template ); You could hook into that filter, handle the including in a callback function and return FALSE. Sample code, not tested: add_filter( 'template_include', function( $template ) { get_header(); ...


11

If you link to the JS file just using something like //examplesite.com/js/jsfile.js without the http:// or https:// the browser should automatically get the correct http or https version


10

found solution: before wp_head() command, insert: remove_action('wp_head', 'rel_canonical'); p.s. if generator meta tag is being added from elsewhere (i.e. from theme or plugin, rather than wp-core) and was priority other than 10, then you might need to put the exact priority, as is was given from that theme/plugin: i.e. remove_action('wp_head', '...


10

Just add to your theme this code: <?php get_search_form(); ?> This code will echoing search form so place it everywhere where you want to have the search form. Further you need to have search.php file in your theme which will show the search results. You can use ordinary WP loop in the file.


10

There's a filter for that: function wpse_203745_wp_headers( $headers ) { $headers['Expires'] = gmdate( $somedate ) . ' GMT'; return $headers; } add_filter( 'wp_headers', 'wpse_203745_wp_headers' );


10

Ok so I think I figured it out... Both of these work for the most part as you'd expect by sending http / php headers to the browser. wp_headers is actually a filter inside the send_headers() functions.You can use this to modify the headers before they're sent to the browser with some exception. wp_headers won't be able to do anything if it's a static ...


9

<?php add_filter('after_setup_theme', 'remove_redundant_shortlink'); function remove_redundant_shortlink() { // remove HTML meta tag // <link rel='shortlink' href='http://example.com/?p=25' /> remove_action('wp_head', 'wp_shortlink_wp_head', 10); // remove HTTP header // Link: <https://example.com/?p=25>; rel=shortlink ...


8

add_action('wp_enqueue_scripts', 'no_more_jquery'); function no_more_jquery(){ wp_deregister_script('jquery'); } That will deregister jquery. But why wouldn't you want jQuery at all? If you mean to simply use your own, you should do it in that function, like this: add_action('wp_enqueue_scripts', 'no_more_jquery'); function no_more_jquery(){ ...


8

BODA82's answer helped, but eventually I realized that I should have replaced responseText with responseJSON method in my JavaScript code. In the example below I was storing the Ajax response results in a variable. I didn't know there was a specific method to get the response in JSON. In a such way the object/array with get_posts() results is returned ...


8

You have to use wp_redirect() before get_header() Then it will not show header error.


8

I had a quick look and here's how the headers are set in the WP_REST_Posts_Controller::get_items() method: $response = rest_ensure_response( $posts ); // ... $response->header( 'X-WP-Total', (int) $total_posts ); $response->header( 'X-WP-TotalPages', (int) $max_pages ); // ... return $response; where: $total_posts = $posts_query->found_posts; ...


8

You should enqueue the script in child theme's functions.php. for example if name of the js file is custom.js and if you place it under js folder in your child theme, then in functions.php you should add function my_custom_scripts() { wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/custom.js', array( 'jquery' ),'',true ); } ...


7

The article on Ghacks is actually a pretty silly way to do it as well. The get_header() function is actually a pretty smart function. You can do some neat things with it. For example, you can do this: get_header('category'); That will cause it to load the header-category.php file, if such a file exists, or the header.php file, if header-category.php does ...


7

That's an old question, here is another and easy solution for WORDPRESS MULTISITE MENU sharing across all network sites, Not only menu you can use the same method to share anything other then widgets across all the network sites. here is the solution : Edit your Header.php //store the current blog_id - Use this function at the start of the function that ...


7

The proper way to send a status (when WordPress is not available) is: http_response_code( 403 ); See the PHP Manual for its definition. But in Plugin files, this should never be the "default" code on top of a file header. See Worthwhile to restrict direct access of theme files? for a discussion. In WordPress, use status_header( 403 ) if you need it. A ...


6

Yes, you open your site to being requested via AJAX to any other script in the whole web. It would be better if you limit the origin to one specific remote domain from which you are consuming the API, like this example: header("Access-Control-Allow-Origin: http://mozilla.com"); However as the mozilla documentation states, a client can fork the origin, ...


5

Actually, my recommendation would be to do things a bit differently. You can add a custom rewrite endpoint to WordPress to handle these files specifically. For example, the URL http://site.com/download-xml/the_filename would automatically download the specified file as an attachment. First, you need to add a custom rewrite endpoint to set this up: ...


5

WordPress offers the function status_header() to return the correct status-code. You can call this function inside your WordPress template/function: // Will return http status header "200 OK" status_header(200);


5

Wordpress provides a way to prevent the header HTML from being rendered, by appending &noheader=true to the url. That will cause the header HTML to wait for you to call it manually, so that you can do a redirect before that. To later render the header HTML from your page, you'll have to use this: if ( isset($_GET['noheader']) ) { require_once(...


5

As you said per page/post basis, this would work for each post add_action('wp_head', 'add_link_in_head'); function add_link_in_head() { global $post; if(!empty($post)) { $alternate = get_post_meta($post->ID, 'alternate', true); $hreflang = get_post_meta($post->ID, 'hreflang', true); if(!empty($alternate) && !...


5

get_header() accepts an argument, using it you can call a different headers. The only thing that get_header() does, is to include in the template where is called the file 'header.php' from child theme (if present) or from theme. If you use the argument $name, like so: get_header( $name), the function will look for a file named 'header-{$name}.php'. An ...


5

Your method is called too late. I don't know how you call this method but you need to run it before output is sent to the browser-- usually that means before get_header(). There are a number of hooks that can be used. For example (from https://wordpress.stackexchange.com/a/131210/21376): add_action( 'template_redirect', function() { if (is_single()) ...


5

Say if your have header-video.php used for video post format. Replace get_header(); with get_header(get_post_format()); in your regular singular.php or index.php. If WP can find header-video.php it will load it otherwise it will automatically will fallback to header.php.


5

Background The infamous "Headers already sent" error rears it's ugly head in circumstances where something attempts to modify the HTTP headers for the server's response after they have already been dispatched to the browser - that is to say, when the server should only be generating the body of the response. This often happens in one of two ways: ...


Only top voted, non community-wiki answers of a minimum length are eligible