I'm writing an Wordpress plugin. With this plugin I update some data. The query and updating works fine, but my header("location: url"); doesn't work. If I place an echo, it won't give any error that the headers already send. It looks it doesn't do anything with those lines. My code...
<?php require_once('../../../wp-config.php');
$baanstatus_table=$wpdb->prefix . 'baanstatus';
$id = $_GET['id'];
$bijgewerkt =$_GET['bijgewerkt'];
$baanstatus= $_GET['baanstatus'];
$handicarts = $_GET['handicarts'];
$trolleys = $_GET['trolleys'];
$winterontheffing = $_GET['winterontheffing'];
$zomergreens = $_GET['zomergreens'];
$qualifying = $_GET['qualifying'];
$onderhoud_greens = $_GET['onderhoud_greens'];
$onderhoud_anders = $_GET['onderhoud_anders'];
$opmerkingen = $_GET['opmerkingen'];
global $wpdb;
$data_array =array('id' => $id,
'bijgewerkt' => $bijgewerkt,
'baanstatus' => $baanstatus,
'handicarts' => $handicarts,
'trolleys' => $trolleys,
'winterontheffing' =>$winterontheffing,
'zomergreens' =>$zomergreens,
'qualifying' =>$qualifying,
'onderhoud_greens' =>$onderhoud_greens,
'onderhoud_anders' =>$onderhoud_anders,
'opmerkingen' =>$opmerkingen
);
$where =array('id' => $id);
$wpdb->update( $baanstatus_table, $data_array, $where );
header("location:http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier");
exit();
?>
Perhaps you should try the javascript, instead of PHP location.
<?php
echo '<script>location.href="http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier";</script>';
?>
The below code will help you
<?php
wp_redirect( $location, $status );
exit;
?>
The above wordpress function will use to redirect Codex Link function reference
You should hook your plugin to a proper Wordpress action to avoid the "headers already sent error" when trying to redirect.
I found that a good place to perform redirects is the template_redirect action, so you can write something like this:
function do_something_then_redirect() {
// do something with $_GET or $_POST data
// then redirect to some url defined in the $redirect_url variable
wp_redirect($redirect_url);
die;
}
add_action('template_redirect', 'do_something_then_redirect');
Related
I want to edit this link via wordpress
http://domain-name.com/self-coaching-tips/media/
when I click on edit, I am able to edit the MEDIA part only. I want to update the whole link to
http://domain-name.com/media/
How can I do this?
Add to your functions.php file:
function cstm_url_redirects() {
$redirect_rules = array(
array('old'=>'/self-coaching-tips/media/','new'=>'/media/'),
//array('old'=>'/some-other-old/page/','new'=>'/some/new/page/'),
);
foreach( $redirect_rules as $rule ) :
// if URL of request matches with the one from the array, then redirect
if( urldecode($_SERVER['REQUEST_URI']) == $rule['old'] ) :
wp_redirect( site_url( $rule['new']) , 301 );
exit();
endif;
endforeach;
}
add_action('template_redirect', 'cstm_url_redirects');
More examples here
Some of my wordpress posts have a custom field called a QR shortcode. I want to be able to redirect any URL request when someone goes to domain.com/QRCODEVALUE, and have them directed to the post with the corresponding QR code. Is there any way to do this? I don't even know where to start!
I haven't tested this, but the logic should help you get somewhere. Grab the URL request path, remove the trailing slash if it exists, and query posts where the custom field contains QRCODEVALUE:
<?php
function my_redirect() {
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
$path = explode('/', $path);
$length = count($path);
$result = $path[$length-1];
$id;
$query = new WP_Query( array('meta_key' => 'QRCODEVALUE', 'meta_value' => $result) );
if($query->have_posts()): while($query->have_posts()): $query->the_post();
$id = $post->ID;
endwhile; endif;
wp_redirect( get_permalink( $id ) );
exit;
}
add_action('template_redirect', 'my_redirect');
Reference: Get Everything After Domain Name into a string
Is domain.com/QRCODEVALUE a page? If so, you could do something like this:
function my_redirect() {
if ( is_page( 'QRCODEVALUE' ) ) {
// Get the ID of the post with the corresponding QR code here
// Save the post ID to $post_id
wp_redirect( get_permalink( $post_id ) );
exit;
}
}
add_action( 'template_redirect', 'my_redirect' );
Trying to get my post meta from posts using shrotcodes and then displaying it on the content.This is the code that's trying to do this:
$string = '';
$custom_content = get_post_custom($post->ID);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$content = get_the_content();
$bonus = $custom_content["bonus"];
$string .= $content . $bonus . '<br>';
endwhile;
}
return $string;
It's not working as the custom content returns empty. Whats wrong? Thanks in advance
I don't think you got the shortcode idea, you should read some info about add_shortcode() also you can use get_post_meta() to retriev the metadata from the database.
Here is an example on how you can achieve this but this will only work with the main loop (as default):
<?php
//you can put this code in functions.php or you can build a plugin for it
function metadata_in_content($attr) {
//this is the function that will be triggerd when the code finds the proper shortcode in the content; $attr is the parameter passed throw the shortcode (leave null for now)
global $wpdb, $wp_query;
//we need global $wpdb to query the database and to get the curent post info
if (is_object($wp_query->post)) {
$post_id = $wp_query->post->post_id;// here we save the post id
$metadata = get_post_meta( $post_id, $key, $single ); // here we get the needed meta, make sure you place the correct $key here, also if you don't want to get an array as response pass $single as "true"
return $metadata; // this finally replaces the shortcode with it's value
}
}
add_shortcode('insert_metadata', 'metadata_in_content');
//the above code hooks the metadata_in_content function to the [insert_metadata] shortcode
?>
Now all it's left to do is to place [insert_metadata] in the post content and things should work.
I have developed a function in functions.php to add a specific id to the body tag depending on the page template or the parent page template if it's a child page.
function get_body_id() {
global $post;
$parents = get_post_ancestors( $post->ID );
$parent_id = ($parents) ? $parents[count($parents)-1]: $post->ID;
$temp = get_page_template_slug($parent_id);
$path = pathinfo($temp);
$temp = $path['filename'];
switch ( $temp ):
case 'page-recipe-landing':
$id = 'bg-green';
break;
case 'page-home':
default:
$id = 'bg-pink';
break;
endswitch;
echo "id='".$id."'";
}
This works well for pages and their children but for my custom post types events/news and recipes this obviously will not return anything for get_post_ancestors( $post->ID ).
Is there a way to alter my function to cater for these custom post types or to assign custom post types to a specific 'page'?
Thanks for help with this.
<?php
if(empty($parents)) {
$loop = new WP_Query(array('post_type' => 'NAME_OF_TYPE', 'post_status' => 'publish'));
while($loop->have_posts()): $loop->the_post();
the_ID();
endwhile;
}
?>
This code should at least output the ID of each post with a custom type of NAME_OF_TYPE. If you wanted to store those in an array and work with them from there, the premise would be pretty much the same. I haven't tested the code, but it should work with custom post types.
I would like to display search results grouped by post type. I have regular posts, pages,
and a custom post type of product. How would I accomplish this by editing the below code.
The code below just shows all posts and pages right now.
<?php
while (have_posts()) : the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
?>
This code alters the original search query to order the results by post-type in the order you select. There are other solutions, but this is the only one i found that doesn't break pagination or requires multiple queries.
add_filter('posts_orderby', 'my_custom_orderby', 10, 2);
function my_custom_orderby($orderby_statement, $object) {
global $wpdb;
if (!is_search())
return $orderby_statement;
// Disable this filter for future queries (only use this filter for the main query in a search page)
remove_filter(current_filter(), __FUNCTION__);
$orderby_statement = "FIELD(".$wpdb - > prefix.
"posts.post_type, 'post-type-c', 'post-type-example-a', 'custom-post-type-b') ASC";
return $orderby_statement;
}
In your case, I'd do two things:
filter the search page's initial query to a particular post type
use one WP_Query call for each remaining post type
For (1), this would go in your functions.php:
<?php
function SearchFilter($query) {
if ($query->is_search && !is_admin()) {
if (isset($query->query["post_type"])) {
$query->set('post_type', $query->query["post_type"]);
} else {
$query->set('post_type', 'product');
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>
For (2), adapt the code you provided from your template file:
<?php
$s = isset($_GET["s"]) ? $_GET["s"] : "";
$posts = new WP_Query("s=$s&post_type=post");
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
echo "<h1>";
echo $post->post_type;
echo $post->post_title;
echo "</h1>";
endwhile;
wp_reset_postdata();
endif;
?>
You can re-use this code for each other post type.
It's best to avoid using query_posts... see querying posts without query_posts (even WordPress devs agree).
You need to alter the post query to reorder things. You would execute this just before you enter the loop. You can read more about query_posts in the Wordpress codex.
http://codex.wordpress.org/Function_Reference/query_posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => array('type1', 'type2') ) );
query_posts( $args );
//the loop