Hot answers tagged

19 votes
Accepted

wp_redirect() - headers already sent

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', '...
user avatar
  • 635
17 votes
Accepted

How to remove rest api link: in http headers?

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 ...
user avatar
  • 9,439
17 votes

Adding javascript to child theme

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 ...
user avatar
  • 1,067
16 votes

Is there a way to enable Cross-Origin Resource Sharing for WordPress' ajaxurl?

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 ) { ...
user avatar
  • 269
15 votes

wp_headers vs send_headers. When to use each?

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() ...
user avatar
  • 4,169
11 votes
Accepted

Auto get_header and get_footer on every template?

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, ...
user avatar
  • 105k
11 votes

Loading different JS for http and https

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
user avatar
11 votes

wp_redirect() - headers already sent

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

How do I add a search bar to my header?

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 ...
user avatar
  • 1,184
10 votes
Accepted

Recommended location to set response headers?

There's a filter for that: function wpse_203745_wp_headers( $headers ) { $headers['Expires'] = gmdate( $somedate ) . ' GMT'; return $headers; } add_filter( 'wp_headers', '...
user avatar
  • 35.5k
10 votes
Accepted

Clarity needed on usage of multiple 403 forbidden header() functions at the beginning of the plugin files

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 ...
user avatar
  • 105k
9 votes
Accepted

Custom endpoint and X-WP-TotalPages (headers)

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)...
user avatar
  • 64k
8 votes
Accepted

Can't get a JSON object in response to an Ajax request with wp_ajax

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 ...
user avatar
  • 1,734
8 votes
Accepted

Cannot load admin-ajax.php. No access-control allow origin*

There are filters for allowed_http_origins and add_allowed_origins. You can use them to set the proper Access-Control-Allow-Origin header in the response to your AJAX call. Add this to your theme's ...
user avatar
  • 96
7 votes

Sharing Header Components Across Multisite

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 ...
user avatar
7 votes
Accepted

How can I change HTTP headers only to posts of a specific category from a plugin

Why it doesn't work: send_headers hook is fired once the HTTP headers for caching, content etc. have been sent, but before the main query is properly initiated. That's why in_category() function doesn'...
user avatar
  • 7,174
6 votes

How do I make my plugin load before the headers are output so I can redirect wordpress?

The correct hook to use is template_redirect which allows you to have the necessary info available to do checks while also early enough to actually redirect. As per the example on the codex page: ...
user avatar
6 votes
Accepted

Is it safe to fix Access-Control-Allow-Origin (CORS origin) errors with a php header directive?

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,...
user avatar
6 votes
Accepted

Is_front_page inside header.php is always returning true

Yes it should work in the header file just like normal. Try using a standard IF statement instead of shorthand: if( is_front_page() ){ echo 'Front page!'; } You must have it configured in the ...
user avatar
  • 2,693
5 votes

The plugin generated 80 characters of unexpected output!

USE DEBUG: (creates log file error_activation.txt in your plugin folder) add_action('activated_plugin','my_save_error'); function my_save_error() { file_put_contents(dirname(__file__).'/...
user avatar
  • 5,281
5 votes

How do you log a user out with wp_logout?

If you're using wp_logout in your own code, its probably best to exit or wp_redirect immediately afterwards. You can call wp_set_current_user(0) after wp_logout() to manually log the user out ...
user avatar
5 votes
Accepted

How can I use CURLOPT_USERPWD in wp_remote_post?

Use the Authorization header. Example: $auth = base64_encode( $username . ':' . $password ); $args = [ 'headers' => [ 'Authorization' => "Basic $auth" ], 'body' => $...
user avatar
  • 105k
5 votes
Accepted

How to load a different header based on post format?

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 ...
user avatar
  • 1,139
5 votes
Accepted

'Headers already sent' Error When Redirecting from add_submenu_page() Callback

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 ...
user avatar
  • 5,340
5 votes
Accepted

add styles if custom header was included

get_header has an action hook called, (surprise!) get_header which gets a single parameter - the name of the header file. So what we do is check that name and if it matches our name we add a style. So ...
user avatar
  • 5,430
5 votes
Accepted

How to remove X-Frame-Options: SAMEORIGIN" from WordPress?

Check this question How does wordpress restrict X-FRAME to sameorigin?. The questioner's issue was resolved by modifying his site's .htaccess file by adding the below line to it as his Web Host set ...
user avatar
4 votes

WP function/filter for modifying http headers?

send_headers is the preferred method over wp_headers for this situation as demonstrated in the codex. is_admin() || add_action('send_headers', function(){ header('X-UA-Compatible: IE=edge,chrome=...
user avatar
  • 4,169
4 votes

Programmatically log in a wordpress user

You can try this function as it is working for me function custom_login() { $creds = array(); $creds['user_login'] = 'example'; $creds['user_password'] = 'plaintextpw'; $creds['...
user avatar
4 votes
Accepted

Object method calling for global $wpdb in header.php

I think the problem might lie in: $wpdbinfo = $wpdb->get_results("SELECT * FROM bo_mytable WHERE id=3"); $wpdb->get_results() returns an array of objects, yet you are referencing a property on ...
user avatar

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