Wordpress filter from variable? - php

I can't remember how to structure this correctly, but I'm trying to change the output of a get_option setting within a Wordpress site. I can get the variable output correctly, but I can't remember what I need to do to get it to filter the output and updated within the apply_filters function.
This is what I got so far:
$tab_pos = get_option('tab-items');
add_filter($tab_pos['position'], 'tab_filter');
function new_tab_pos(){
return 'right';
}
apply_filters('tab_filter', 'new_tab_pos');
Basically the $tab_pos['position'] is returning left and I want to change it to right but I can't remember to to get it to hook into that to make the update. I'm also not sure if I need to updated it with a str_replace or something of that sort.

Looks like you have your parameters and function usage the wrong way around... I think you want something along these lines:
<?php
$tab_pos = get_option('tab-items');
$tab_pos['position'] = apply_filters('tab_filter', $tab_pos['position']);
function new_tab_pos($position){
return 'right';
}
add_filter('tab_filter', 'new_tab_pos', 10, 1);

Related

Why does this variable refuse to work like a string?

I am running the following line of code in WordPress' functions.php:
$comment_meta_val = get_comment_meta($num_id, $comment_meta_key, true);
When I set
$num_id = '76'
The code works perfectly. However, instead of '76' if I feed it a variable e.g. $comment_id, it doesn't work, even though I can echo $comment_id and see it is 76.
I've tried using
$num_id = strval($comment_id);
$num_id = (string) $comment_id;
$num_id = "$comment_id";
But none of the above work. It's probably something really stupid I'm doing wrong, but I've been up against this for the better part of the day and finally must accept I need help!
The issue was in the action hook that was saving the metadata of a comment.
I had to add a new action in, which populates the comment meta into an array, and runs before the function I mentioned in my question.

echo function call 2 variables

Ok so I have the code for a form that is called and works but it needs two varibles grabbed from the string of a url. I have the first and the second is the same for what im doing on any page that I am creating which is alot. Here is the code at the url: collabedit.com/9g99j
Question if Get <?php echo $_GET['id']; ?> is grabbing my id string from the url how do I use this in the echo of my function I just defined at the bottom of the code? Instead of having this: echo DescriptionGet(1256124, 50874); can someone tell me how to put something like this: echo DescriptionGet(1256124, $id);
This would make it so i dont' have to enter that id value for every page I want to create.
Thanks,
Thanks everyone for your replies and I was able to figure it out on my own and actually used exactly what the first reply was.
Now I have a new question about this function. How do I make it grab the image from that same page its grabbing the form code from? I can't figure this part out and its keeping me from doing mass automation for this site.
Anyone help?
Try this:
$id = $_GET['id'];
echo DescriptionGet(1256124, $id);
You can change your function definition from:
function DescriptionGet($c, $id)
to
function DescriptionGet($c, $id=50874)
Each time when you will call DescriptionGet($c) it will behave as you passed $id=50874 but also if you need you can call DescriptionGet($c, 20) and $id in the function will be set to 20.
And in case you want to simple use $_GET['id'] as function parameter you can simple run
echo DescriptionGet(1256124, intval($_GET['id']));
you don't even need to use extra variable.

How to set $userID from queried object ID in Wordpress

I am editing a template to try and add some conditional logic to my page.
The page template shows topics related to a user.
I want to add a piece of code which will grab the user name from the page we are viewing and then use that in a string for my conditional statements.
The code I have put together is as follows, but it breaks my page so I am doing something wrong.
<?php global
// I query the ID and try and set that to the $userID - I think I am doing this wrong, but when I echo the ID it gets the correct info.
$userID = get_queried_object()->ID;
// This is the string I create using the userID which should be from the query above
$memberstatus = get_user_meta($userID,'member_status',true);
?>
later on I use IF statements to use thsi result (which i know work) so i won't post them. My problem is trying to get the above to work.
Any help?
damm, looks like when I remove 'global' from the php it works! I thought global had to be in this...ah well

set value to display information

I have a piece of code I can't figure out how it actually works.
Its flow is like this, looks very easy
Get information to display for example $info
Display information using $this->set('columnname', $info);
Is that set function built-in in cakephp? columnname can also be page's content/type. this set will display info in the page. Where is that page source I need to see? For example perhaps it is stored somewhere in some view that has something like <span id=x></span> it then may only need to get the id and replace something inside the span.
Yes set is built-in in cakephp.
You set variables from controller to pass it to view, like in your controller
$this->set("your_info", $info);
Then in view you can access it as:
echo "This is the info set from controller:".$your_info;
//or if its an array, do
print_r($your_info);
Did you mean something like that. Hope it helps

PHP Query String Manipulation

I have a small issue with manipulating the current URL query string to add an extra parameter at the end.
Per example, say there's a category layout for products, the URL would be:
index.php?category=3&type=5
Now, on that page I have a link for a layout that is either a table or a grid. In those URLs I currently have:
<a href="index.php?<?php echo preg_replace($array,'',$_SERVER['QUERY_STRING']); ?>&layout=grid" ...
Then, I do the same for the table href as well. Also in my array I have just:
$array = array ( '/&layout=table/', '/&layout=grid/' )
Is this the right way, or is there a better way for doing this? I'm asking because without preg_replace, it will continue adding that same layout parameter everytime it is clicked, so it will also show the previous parameter, then the next, then the next.. without removing the previous layout parameters.
Any insight on this will be much appreciated.
EDIT:
Thanks to the answers below, I have created a little function:
function buildQuery($key,$value) {
$params = $_GET;
$params[$key] = $value;
return http_build_query($params);
}
Then its only a matter off:
grid
this might seem pointless but i like to have my view / template files without the extra set vars. Im a clean freak. I might even return the 'index.php?' with it just so i can be more lazy, anyways something to play with now :)..
If you want to modify the query string, it's easier to simply modify the GET variables and rebuild the query string:
$params = $_GET;
$params['layout'] = 'new_layout';
Then:
...
Although you could also do:
...
Think about directly parsing the $_GET paramaters to build your url.
I think what you want to do is have the link going to index.php with all the same parameters as you have at the moment, but changing layout to grid. I'd suggest you do something like this:
<?php
// make a copy of the $_GET array with all the parameters from the query string
$params = $_GET;
// set layout=grid regardless of whether layout was set before or its value
$params['layout'] = 'grid';
// generate a query string to append to your urls.
// Note that & is used as the arg separator; this is necessary for XHTML and advised for HTML
$queryString = http_build_query($params, '', '&');
?>
href="index.php?<?php echo $queryString; ?>">
This is much easier than trying to edit and fix the $_SERVER['QUERY_STRING'] yourself.

Categories