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'; ...


16

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', '...


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

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....


12

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 ...


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

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', '...


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


11

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 ); } ...


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

<?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 ...


9

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 ...


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; ...


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 ...


6

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 functions.php file: add_filter('allowed_http_origins', 'add_allowed_origins'); function add_allowed_origins($origins) { $origins[] = 'https://www....


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, ...


6

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 settings to use: A static page (select below) instead of Your latest posts If you want is_front_page() to only return TRUE when viewing the page you select ...


6

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't work when you use it in this action hook. Same goes for the wp_headers filter hook. To make it work: You may use the template_redirect action hook (or even ...


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

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__).'/error_activation.txt', ob_get_contents()); }


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

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: function my_page_template_redirect() { if( is_page( 'goodies' ) && ! is_user_logged_in() ) { wp_redirect( home_url( '/signup/' ) ); ...


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.


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