need the right way to setcookie in wordpress - wordpress

i've been looking the whole day how to setcookies in wordpress. in my way i found out (using the developer toolbar) that the cookie is set but still not working.
i have 2 files the first contains the login form redirecting to another page to set the cookie and return to another page to check if it's working. domain which is tested on is like this : blog.mydomain.com. here's the setcookie file :
<?php
setcookie("user_name","test",time()+3600);
?>
and chcking the cookie like this :
if(isset($_COOKIE["user_name"])){
echo "cookie exists";
}
else{
echo "cookie doesn't exist";
}
i've read many topics about this issue but there was no clear answer.
Thanks in advance

This typically happens when you try to set a cookie after sending output to the browser. To set a cookie in WP, you should use the 'init' hook to set the cookie on init.
function set_username_cookie() {
if (!isset($_COOKIE['user_name'])) {
setcookie("user_name","test",time()+3600);
}
}
add_action( 'init', 'set_username_cookie');

well, my best way to use cookie in wordpress is this,
function set_my_cookie() {
global $post;
$post_id = $post->ID;
$cookie_name = "my_cookie";
$cookie_value = "my_cookie_val";
if (!isset($_COOKIE['my_cookie'])) {
{
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
}}add_action( 'wp', 'set_my_cookie');
i used this the function to setcookie in wp hook of wordpress. the main reason of this is that we may need sometime current page or post, that we cannot access on init hook, but we can access in wp hook.
now, in a shortcode or other plugin/theme functions we may just need to check if the cookie exists or not. thats it

Another option is to use PHP's ob_start(); and ob_end_flush();.
You can find documentation on the two functions here
The way I resolved my issues was to call the two functions before and after the opening and closing html tags like this:
<?php ob_start(); ?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php /* Wordpress loop and other tempate code here */ ?>
</body>
</html>
<?php ob_end_flush(); ?>
The issue I was running into was calling a global function that used PHP's setcookie(); and because WordPress processes the page progressively, the cookie couldn't be created due to the page's headers already being sent.
PHP's output buffering function forces the headers to be sent before WordPress processes the page.
Hope this helps.

Related

Zaki Like Dislike Comments plugin cause headers already send error

When using wordpress Zaki Like Dislike Comments plugin on php 5.3 server, I received
Cannot modify header information - headers already sent by (output
started at
/home/otzyvdom/domatop.ru/docs/wp-content/plugins/zaki-like-dislike-comments/classes/class-zaki-like-dislike.php:169)
That is why Zaki Like Dislike Comments uses add_action('comment_text','ZakiLikeDislike_AddPluginHml'); hook to add like/dislike buttons in comment text. But the comment_text event occures not only when post with comments is shown but during comment form proceeding etc. And ZakiLikeDislike_AddPluginHml function makes echo instead of modifying comment content!
Finally to avoid this error you must modify main function like this:
function ZakiLikeDislike_AddPluginHml($content) {
$settings = get_option('zaki_like_dislike_options');
ob_start();
if($settings['show']) :
echo ZakiLikeDislike::getLikeDislikeHtml();
endif;
$out = ob_get_contents();
ob_end_clean();
return $out.$content;
}

Redirect outside WP after Login

I have http://mysite.com/admin.php
There I check wether the user is admin or not.
In second case I send the user to the wp-login page like this:
blog.mysite.com/wp-login.php?redirect_to=http%3A%2F%2Fmysite.com/admin.php
I expect redirect back for admin.php but wordpress always send me to wp-admin control panel.
I have researched.
When the dest. host is not in filter
allowed_redirect_hosts
WP just redirect the user to wp-admin.
How can I add more hosts to the filter?
If I put this example from the WP Codex on functions.php it stops working.
(http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts)
add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$content[] = 'blog.example.com';
$content[] = 'codex.example.com';
// wrong: $content[] = 'http://codex.example.com';
return $content;
}
Add the following in your functions.php:
function my_allowed_redirect_hosts($allowed_host) {
$allowed_host[] = 'anothersite.com';
$allowed_host[] = 'www.someotherwebsite.com';
return $allowed_host;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
Replace anothersite.com and add new values accordingly.
If you're trying to redirect users in a normal page, you can make use of Wordpress's wp_redirect() function:
<?php
wp_redirect( $location, $status );
exit;
?>
Documentation: wp_redirect()
Hope this helps!
Finally it works!
What I was doing wrong is putting the code in the WP functions.php file, and not in my custom theme functions.php file.
Thanks all!

How to redirect all wordpress posts and pages to a new domain.?

i need a 301 redirect
www.old-domain.com/simple-post/ to go to www.new-domain.com (not new-domain.com/simple-post)
I tried using some .htaccess rules and did nothing,
mod-rewrite is enabled
thanks
If you are transfering your WordPress website from a domain to another, this is the way to go :
http://codex.wordpress.org/Moving_WordPress
Almost all the time theses lines do the trick :
//FIXME: do comment/remove these hack lines. (once the database is updated)
update_option('siteurl', 'http://your.domain.name/the/path' );
update_option('home', 'http://your.domain.name/the/path' );
Of course you also need to change every static links inside your posts and/or template files.
Here is a link that could help replacing links in content :
http://lorelle.wordpress.com/2005/12/01/search-and-replace-in-wordpress-mysql-database/
put this code on header.php
pass in "get_the_ID(1)" post(simple-post)id.
set redirect url.
<?php if (get_the_ID(1)) { ?>
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
<?php } ?>

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

Way to get Twitter button, with count, with custom bit.ly URL, working?

I'm stuck. I posted this on WordPress.StackExchange and they suggested I try at WebApps.StackExchange, and they suggested I try here. So, apologies for the multiple posts if you follow all those!
I have a client blog using bit.ly pro to generate custom short urls (ie foo.co). I want to show the regular horizontal version of the Twitter button, with tweet-count, and have the link that goes to the post use their custom bit.ly pro url.
I have installed Joost de Valk's Bit.ly Shortlinks plugin, which successfully converts normal WP shortlinks (wp_get_shortlink()) to the custom Bit.ly pro URL elsewhere in the site, but Twitter seems to trump that and render everything with the default t.co domain instead.
I've looked at the suggestions from this question but using the # as the data-url doesn't work, and the suggested Twitter support pages don't seem to contain any info on how to get Bit.ly to work (though they say they're going to).
Here's the function I created to insert the button in my theme - any ideas on where I'm going wrong? this is used to insert the button both within the Loop and on single-post pages.
function tweet_this() {
global $post;
ob_start();
$tweet = wp_get_shortlink();
echo '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
return ob_get_clean();
}
In case it helps, this function does work, except it doesn't render the tweet-count:
function tweet_this() {
global $post;
ob_start();
$tweet = sprintf( __('%1$s %2$s'), $post->post_title, wp_get_shortlink() );
echo '<a class="tweethis" href="http://twitter.com/intent/tweet?text=' . urlencode( $tweet ) . ' via #clientname">Tweet this</a>';
return ob_get_clean();
}
Let me know if you need more info - and thanks in advance for any help you can throw my way!
Michelle
function tweet_this() {
global $post;
$tweet = get_permalink(); //replace with your code
$tweetmarkup = '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
echo $tweetmarkup;
}
This works for me, but I don't have the WPShortlinks installed, so I replaced it with the permalink. You should be able to replace the permalink with your wp_get_shortlink and it should work.

Resources