WordPress variables and memory

Your installation might be … less than optimal. Using my Mini Theme, no plugins and the following code on the front page right after the opening body tag … print count( $GLOBALS ) . ‘ $GLOBALS<br>’; print @count( get_defined_vars(), 1 ) . ‘ variables<br>’; print count( get_defined_constants( TRUE )[‘user’] ) . ‘ constants’; … I … Read more

Where to get information about array fields in $_REQUEST?

This is mostly pure PHP, but it does have WordPress twist. PHP has number of superglobal variables, that contain information relevant to current request. Out of those: $_GET contains info from URL (HTTP GET request) $_POST info from form submission (HTTP POST request) $_COOKIES about cookies set and $_REQUEST is combination of the above (according … Read more

Using $_GET variables in the URL?

Try adding the variable to the WordPress’ array of ‘recognised query variables’… add_filter(‘query_vars’, ‘my_register_query_vars’ ); function my_register_query_vars( $qvars ){ //Add query variable to $qvars array $qvars[] = ‘my_error’; return $qvars; } Then the value of ‘my_error’ can be found via get_query_var(‘my_error’). (See Codex) EDIT From Otto’s comment, it’s better to do: add_action(‘init’,’add_my_error’); function add_my_error() { … Read more

Custom page with variables in url. Nice url with add_rewrite_rule

I think the add_rewrite_tag() is not needed, and can be replaced with adding the variables to the public query vars directly: // Either directly (in your init hook): $wp->add_query_var( ‘var1’ ); $wp->add_query_var( ‘var2’ ); // Or via a filter: add_filter( ‘query_vars’, ‘wpse12965_query_vars’ ); function wpse12965_query_vars( $query_vars ) { $query_vars[] = ‘var1’; $query_vars[] = ‘var2’; return … Read more

How to Pass External Variables to Filters/Actions

You have at least two options: Globalize the desired variable, and then reference it inside the callback Wrap the score calculation logic with a function, then reference it inside the callback Globalize the Variable <?php global $score; $score = 42; //Some crazy calculation I don’t want to repeat. function add_score_to_title($title) { global $score; return ‘Quiz … Read more

What are all the Properties of the WordPress Post Object?

Post object is mostly queried row of wp_posts database table with some extras. It is easy to dump content of one and see: object(stdClass) public ‘ID’ => int public ‘post_author’ => string public ‘post_date’ => string public ‘post_date_gmt’ => string public ‘post_content’ => string public ‘post_title’ => string public ‘post_excerpt’ => string public ‘post_status’ => … Read more

Best way of passing PHP variable between partials?

Basic separated data structures To pass around data, you normally utilize a Model (that’s the “M” in “MVC”). Let’s look at a very simple interface for data. Interfaces are just used as “Recipes” for our building blocks: namespace WeCodeMore\Package\Models; interface ArgsInterface { public function getID(); public function getLabel(); } Above is what we pass around: … Read more

Passing a variable to get_template_part

As posts get their data set up via the_post() (respectively via setup_postdata()) and are therefore accessible through the API (get_the_ID() for e.g.), let’s assume that we are looping through a set of users (as setup_userdata() fills the global variables of the currently logged in user and isn’t useful for this task) and try to display … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)