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.
Related
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);
I'm trying to set cookies for the first time. I've been following the documentation on W3 schools but it doesn't seem to be working for me.
Here is my code (some naming omitted for this example):
$namenospace = 'thisnameexample';
function someSetup($somename){
$nameofcookie = $somename . '_some_value';
if(!isset($_COOKIE[$nameofcookie])) {
$someValue = rand(1, getrandmax());
$cookielastsfor = time() + (86400 * 30);
setcookie($nameofcookie, $someValue, $cookielastsfor, "/");
}
else {
$someValue = $_COOKIE[$nameofcookie];
};
return $someValue;
};
$setSomeValue = someSetup($namenospace);
As you can see, I'm trying to check if a cookie is set, if it is, grab the value and use it, otherwise, set the cookie, then return the value anyway.
For some reason this setcookie() function isn't working. Does anyone have any insight on this?
Possibly worth noting this is a wordpress site if that's relevant?
Thanks!
Edit: have updated as per scope issues highlighted in comments and still not working as intended.
Edit again: definitely not a duplicate. My team and I discovered the issue and would like this unmarked as duplicate so an answer can be given please!
Make sure you are setting your cookies before output. Where are you calling your function?
As an alternative, you could use JS to set a cookie on client side.
Using a plugin I'm able to use PHP on page by using [insert_php] as a tag however, whenever I try using SQL it doesn't seem to work.
I tried using:
global $wpdb;
$prepared = $wpdb->get_row(
"SELECT SiteID, SiteName
FROM $wpdb->Site
WHERE SiteID = 1");
echo $prepared->SiteName;
echo "test";
All I'm getting is test on the page and I've tested to see if my sql statement was at fault and it seems to be working fine so I'm guessing there's an issue with $wpdb or the way I'm outputting the data.
WordPress.org has a lot of detailed information in their reference.
I think attempting to refer to $wpdb->Site is a likely suspect for why your code is not working. You will need to know the exact fields in the table to pull your information.
Here is a reference for the wp_site table. I think you're actually looking for the 'domain' field, not 'sitename'.
Try replacing $wpdb->Site with the actual name of the table. I also get errors like that at first since $wpdb->table_name only works with the default wp tables.
EDIT
It should be something like this:
SELECT SiteID, SiteName FROM Site WHERE SiteID = 1
First, thanks for the time. I have been messing with this quite a bit and cannot figure out what is going wrong (and this is something I thought I understood) I am currently trying to change save location of files uploaded via WP. As the title suggests. The $_GET is not pulling anything from -
/?action=type1&project=1&method=add
I can replace the gets with appropriate numbers and get correct project output so I am almost certain the $_GET that isn't pulling the info. As I understand this is a supervariable so should have no problems getting into a function.
Can anyone let me know if they see any problem in my logic, syntax or am I missing something going on in upload_dir/wp_handle_upload_prefilter hook? Or are there any suggestions on troubleshooting/solving?
$_GET is used everywhere on the site and works fine otherwise.
function ca_doc_pre_upload($file){
add_filter('upload_dir', 'ca_doc_custom_upload_dir');
return $file;}
function ca_doc_custom_upload_dir($path){
if (isset($_GET['project'])) {
$projectID = $_GET['project'];
};
if (isset($_GET['action'])) {
$type = $_GET['action'];
};
$project = ca_get_project($projectID);
$customdir = '/'.strtolower(str_replace(" ", "-", $project->project_name)).'/'.$type;
$path['path'] = str_replace($path['subdir'], '', $path['path']);
$path['url'] = str_replace($path['subdir'], '', $path['url']);
$path['subdir'] = $customdir;
$path['path'] .= $customdir;
$path['url'] .= $customdir;
return $path;}
add_filter('wp_handle_upload_prefilter', 'ca_doc_pre_upload', 2);
To Confirm not able to retrieve with get. Used.
if (isset($_GET['action'])) {
$type = $_GET['action'];
}else{
$type = 'Not Getting';};
And tried to save a file. It saved it to Not getting the folder. (took out the project part).
Some additional information. Created another function in function.php using get and called it on the same page where plupload happens and it worked. The only thing I can figure now it that this isn't working because its called from upload or the hook is doing something... I am only guessing now because I ran out of viable logic a long time ago.
Thanks again for the time.
Have you registered the query params with WP query_vars()? Perhaps the permalink rewriting is interfering..?
https://wordpress.stackexchange.com/a/41373
You may also need to re-save your permalink settings after you add the parameter.
With some other help and research turns out that plupload that handles the upload in Wordpress was doing some AJAX work in the background calling;
url: '<?php echo admin_url('admin-ajax.php')?>'
And for some reason the $_GETs were not going with that, so just changed the URL to include them as follows, and all was right again.
url: '<?php echo admin_url('admin-ajax.php?project='.$_GET["project"].'&action='.$_GET["action"].'') ?>'
Apparently when using the hooks above this is the point(admin-ajax.php) in which it grabs the $_GETS
have you tried
get_query_var('action');
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.