$_GET in Wordpress function.php - Not working - php

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');

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.

PHP function to find matching url from wordpress posts

I'm looking for php code or function which can help me to search and find first matching URL, based on specific pattern, from a wordpress post and echo the same url in the same post where it's necessary instead of modifying parent url.
Example Case:
URL:https://example.com/education_system_comparison.html
when I open this page, afer title of the post, there is description of this post, here I want to search from description to find matching url that must starts from my desired domain name like https://some-url-is-here/3978732978937298.html and ends with .html
If anyone here can help me to create a function or use any filters those can work with wordpress or php, it would be so helping and must appreciated. Thank you so much.
I found the solution what I was looking for and it's working perfectly fine on single post or on custom page.
Here is function, I've added in functions.php of my WordPress theme;
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return ''; }
so I can call above function by using following code and it work 100% fine, example code:
<?php
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$start = ' https://some-url-is-here/';
$end = '"';
$output = getBetween($content,$start,$end);
echo $start.$output; ?>
It gives output like below based on first matching url and stop immediately
https://some-url-is-here/3978732978937298.html
On the other hand, once I applied the same code throughout my website so I can get every matching url from each and every published post to use it where necessary on the same post. As I save changes, my server got halted coz there were hundreds of httpd requests started processing and server went down immediately.
I'm not sure what exactly is going wrong here, If anyone can guide me and help me what exactly wrong here so it'll be much appreciated and any suggestion or fix, Thanks.

Magento change language by login

I'm searching now for hours. I try to switch the store language after the login.
Given is:
The id of the Store I want to switch too.
The Event Observer is also done.
This is what I worked out the last hours
My Observer:
$customerId = Mage::getModel('customer/session')->getCustomer()->getId();
// Get the Store ID we want to switch too
$connection = Mage::getSingleton('core/resource')->getConnection('distributor_read');
$mainLanguage = $connection->fetchAll('SELECT...');
$storeId = $mainLanguage[0]["store_id"];
if (!$storeId == null) {
$storeCode = Mage::app()->getStore($storeId)->getCode();
// Here I have to switch by the store code
return;
}
Would be glad if someone could help me out.
At least I need a method to switch the language or storeview, but I don't find any working MagentoAPI methods.
to Set the store id programatically
In the index.php file, (in your language specific folder), add the following:-
$store_id = 'your_store_id_here';
$mageRunCode = 'store view code';
$mageRunType = 'store';
Mage::app()->setCurrentStore($store_id);
Mage::run($mageRunCode, $mageRunType);
I am suggesting you to create some temporary session variable from login action and read in index.php to set language pack and again unset it if your work has been done
Hope someone will find this information useful :)
I will tell you guys what I did for this case. I tried to get the Mage_Core_Controller_Response_Http class, completely in vain.
So I kept going on my research and I found a solution.
I used:
header('Location: '. Mage::app()->getStore()->getBaseUrl().'/customer/account?___store='.$storeCode);
There we go, my on login observer just set the language.
edit:
To set a new header could cause some problems, because if just any piece of html is already rendered you can not set a new header.
I worked sth. else out:
$url = Mage::getUrl('*/*');
$url .= "?___store=" . $storeCode;
$response = Mage::app()->getFrontController()->getResponse();
$response->setRedirect($url);
$response->sendResponse();
exit();
There is obviously still a problem, the exit shouldn't used in good software code, but a simple return does not work, it does not end or kill the observer action.
I still working on a solution to kill the observer in a right way. As I said the observer need to get killed to redirect the url.

vBulletin robot only comes some of the time when called

I have been doing a lot of modification of my vBulletin forums, and I've taken particular interest in different forms of AI and botting on the forums. I recently created a plugin that will make the bot post in a thread if it is called. It works some of the time, and doesn't other times. I can't figure out why it's so unreliable.
It fires at the hook location "newpost_complete" and has the following code:
if (stristr($postinfo['pagetext'],'.robot:')){
preg_match('#^(?:.robot:)?([^:]+)#i',$postinfo['pagetext'], $matches);
$host = $matches[1];
require_once(DIR . '/includes/functions_robot.php');
run($host,$threadinfo['threadid']);
}
I'm not good with regex so I am not sure that preg_match is optimal. I've found that it rarely runs the code if you post .robot:hi: but if you quote a post with .robot:hi: in it, it will run without fail even if the actual quoted content is changed to something else.
Here's the relevant code in the functions_robot.php file:
function run($command,$threadid) {
global $vbulletin;
global $db;
if ($command == 'hi') {
$output = 'Hello.';
//Queries
}
}
Any ideas on what's causing it to be so unreliable? There's a lot of potential if I can get it running smoothly.
I was able to figure it out with the use of http://regex.larsolavtorvik.com/
I switched to postdata_presave hook instead of newpost_complete.
$pagetext =& $this->fetch_field('pagetext', 'post');
$threadid =& $this->fetch_field('threadid', 'post');
if (stristr($pagetext,'.robot:')){
preg_match('/(\.robot:)(.*)(:)/iU',$pagetext, $matches);
$host = $matches[2];
require_once(DIR . '/includes/functions_robot.php');
run($host,$threadid);
}
The new hook location meant it was usually firing off quicker than the insert on my actual post, making the robot post before me. I fixed this by adding usleep(500000); to the start of my run() function.

URL querystring with a php include

I'm trying to include a file to output in a tab on a page. The file itself will pull up just fine, but when I try to add the required querystring to it, it gives me a "failed to open stream: No such file or directory" error.
I've tried just a straight include and tried setting the querystring as a variable. Here's where I'm at right now.
$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");
Has anyone successfully done this?
You can't include a query string in an include().
Assuming this is a local script, you could use:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
if it's a remote script on a different server, don't use include.
I created a variable on the 2nd page - and passed it a value on the first page - and it worked for me:
*Page with include: 'index.php'
<?php $type= 'simple'; include('includes/contactform.php'); ?>
*Page included: 'includes/contactform.php'
switch($type){
case 'simple':
//Do something simple
break;
default:
//Do something else
break;
}
I modify the accepted answer given by Frank Farmer a bit to work for different queries:
Include twice would cause problem:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
//changing the v to another
$_REQUEST['v'] = 'agent2';
include("agentview.php");
For those who encounter this multiple include problem, you could wrap you code inside "agentview.php" in a function:
Inside agentview.php
function abc($mls,$lid,$v){
...your original codes here...
}
file need to call agentview.php
include_once("agentview.php");
abc($_REQUEST['mlid'], 0, 'agent');
abc($_REQUEST['mlid'], 0, 'agent2');
Hope it helps someone encounter the same problem like me and thanks Frank Farmer for the great solution which saved me alot of time.

Categories