paulsimonrough
Forum Replies Created
-
Forum: Plugins
In reply to: [Fake Who's Online for WordPress] HostingClick here to sign up for UK based web hosting
Forum: Hacks
In reply to: Trying to set cookie on page loadThanks for your reply again! I already have code that deals with the cookie if it’s already been set so I don’t mind if a new number is generated each time.
When you said get the values out of the options table, do you mean the database? if so does WordPress have API for this?
One thing I’m not sure about is, will a value only be set in the database when the user clicks save?
In the back end widget settings:
$title = ! empty( $instance['variableiwant1'] ) ? $instance['title'] : __( '3' );The default value here is ‘3’ but is that set in the database or is just a visual value the end user see’s in the backend and then only when they hit save is a proper value stored? I remember reading this somewhere.
Also if I were to get values from the back end how do I go about getting the widget ID, I’m sure I read you need it to identify the table, if it’s like Joomla then a unique ID is set when you install widgets so I need a way to find that too.
I’m nearly there with this particular extension its just this tiny bit with the cookie I’m needing to sort out. phew…
Thanks again, Paul.
Forum: Hacks
In reply to: Trying to set cookie on page loadThanks for your reply your the first person in 2 weeks who has been able to provide some insight to this, I’m a Joomla Dev translating my extensions over to WordPress and I’m struggling a bit with the API.
I have updated my code to show more of what I’m trying to do:
<?php ## Set a test variable outside the class $variabletest1 = '78'; echo $variabletest1; class my_class extends WP_Widget { ## get variable from the backend form settings of widget, the user sets 2 numbers from which I will create a random number, default 3 and 5 function form($instance) { global $instance; $title = ! empty( $instance['variableiwant1'] ) ? $instance['title'] : __( '3' ); $title = ! empty( $instance['variableiwant2'] ) ? $instance['title'] : __( '5' ); } ##update save of widget function update($new_instance, $old_instance) { $instance = $old_instance; $instance['variableiwant1'] = strip_tags($new_instance[ 'variableiwant']); $instance['variableiwant2'] = strip_tags($new_instance[ 'variableiwant']); return $instance; } ##I want to create a random number from the 2 variables set in the backend by the user, this works as expected. function widget($args, $instance) { $variabletest2 = rand(intval($instance['variableiwant1']), intval($instance['variableiwant2'])); echo $variabletest1; ## echos 78 (this was set outside the class) echo $variabletest2; ## echos the random number } ##here I try and set the cookie from the random number generated before headers are sent. function set_newuser_cookie() { global $instance; global $variableiwant; global $variabletest1; global $variabletest2; ## variabletest2 should hold the random number generated. echo $variabletest1; ## echos 78 echo $variabletest2; ## echos NULL if (isset($_COOKIE["variablesave"])) ; else { setcookie("variablesave", $variabletest2, time()+3600); } } ## I have tried other hooks like 'init' but it seems that the cookie is always set BEFORE the random variable is set. add_action( 'send_headers', 'set_newuser_cookie');I’m just trying to get my head around this. In Joomla, backend setting can be assigned to variable and are available always, you could set one as a cookie directly I’m a little lost with the way WordPress executes things and in which order.
Basically all I need is:
1 A user to sets 2 numbers in the back end of the widget.
2 A random number is created between these 2 numbers.
3 The random number is stored as a cookie on page load.Thanks, Paul.
Forum: Hacks
In reply to: Use wordpress widget option (instance) outside the classThe variable I’m trying to get comes from the backend widget settings.
In the code see ‘variableiwant’
Thanks.