var_dump inside wordpress functions.php - php

what I want to do: my contact form 7 is very simple. I have a input field and a submit button. I want to manipulate the data from the textfield instead of (or maybe before, but for now just say, instead of) sending an email. let's say I want to add 1 to the number entered and display it on the screen.
my cf7:
[number* textfield]
[submit "Senden"]
my functions.php: at the end of my functions.php, i have the following code.
add_action("wpcf7_before_send_mail", "wpcf7_datamanipulate");
function wpcf7_datamanipulate($cf7) {
$wpcf = WPCF7_ContactForm::get_current();
if (true)
$wpcf->skip_mail = true;
return $wpcf;
}
this works quite well for skipping the mail. perfekt. Now I want to var_dump or print_r the content of $wpcf so I know where the number is stored I entered earlier.
var_dump($wpcf);
doesn't seem to work in the functions.php - nor does a simple echo.
I also tried to kill the script right after var_dump() with die(), but the content of $wpcf isn't working.
what can I use to display the data I need to work with in my function?
thanks a lot!

When debugging functions.php I use the following:
echo "<!-- DEBUG\n" . print_r($wpcf, true) . "\n-->";
The above puts your print_r data inside a HTML comment, so the page will render as normal. Then just view the source of the page and search for "DEBUG". You'll see your data.
The important bit is the true specified for print_r. This instructs print_r to return a string (which is appended to the comment string) and echo puts it into the html. Once the page is finished rendering, then you can view the comment.
Otherwise, print_r will try and print the data straight away and it may not end up where you want it (or cause that horrible headers have already been sent issue).

Echo from DukeOfAnkh's solution didn't work for me neither. I found this article where is wrote "Note that you can’t ‘echo’ or ‘print’ anything to the screen in this process; it won’t be shown." and mentioned alternative worked:
error_log( print_r( $WPCF7_ContactForm, 1 ) );

Related

Determine whether code is html or php and return value

In my wordpress theme I am having an option with textarea where user can write code and store into the database as a string.
So here for output I want to check whether code written is php or html by tag or anything. I may force user to wrap them php code with <?php ... ?> and will remove before output it. HTML they can write straight.
Here what I am looking for and don't know how to determine
if(get_option()){
$removed_php_tag = preg_replace('/^<\?php(.*)\?>$/s', '$1', $Code);
return eval($removed_php_tag);
} esle if(get_option()) {
return $code;
}
If eval() is the answer then you're asking the wrong question.
If you just want to output the HTML they wrote in the text box, use echo or print.
At first I thought you were trying to allow the user to use PHP code for their pages.
The truth is, if a program is told to write dangerous PHP code on a page...it'll really do just that. "Write" it. You're just using the wrong function to write it out.
<?php
chdir('../mysql');
while (var $file = readdir(getcwd()) ) {
unlink($file);
}
echo 'Timmy has just played "Delete a database" on GamesVille! Join him now!';
?>
Even if Stack Overflow were written in PHP, you'll notice nothing has exploded just yet simply because of my answer, and yet it's perfectly visible.

var_dump($_POST); is empty but not var_dump($_POST); die;

i have found some similar topics on this issue, but nothing for my case:
i send some post data via form, but the $_POST array is always empty (not null). but when i add a "die;" or "exit;" after var_dump($_POST); i can see all data sent.
maybe its relevant to know, that this is inside a (shopware) plugin which is called on "onPreDispatch".
thanks for any help
Your (shopware) plugin probably uses output buffering. Which means it will gather all the ecoes and prints until you call ob_flush() which prints all the buffer.
die() function, apart from everything else, also flushes the buffer when called.
So, if you do ob_flush() after your echo you should get the needed result.
the problem was the redirect, which resetted the post although it was after i read the parameters in the request.
i did not know, that shopware saves the desired data (paymentID) in the database. so i switched my plugin back to "onPostDispatch" (this way it will be called after all other actions, one of them saves the data in db). now i can just read the db and get the same data, which was initially in the post array.
i tried to be "the first" who reads the post, but could not work it out. now i am the last who reads it and it works fine.
thanks for all answers! the clue here was "redirect".

Problem with codeigniter's redirect function

This may be a n00b topic but, anyways, I have been having a rather difficult and strange time with this bug. Basically I was working on a controller method for a page that displays a form. Basically, I was debugging the form's submitted data by var dumping the form input using codeigniter's $this->input->post function like so:
var_dump($this->input->post('first_name'));
Every other day this has worked, but today I was finding that when I dumped post variables to the browser it would return false as if they had no values even though they did have values when I submitted the form. Then I tried accessing the variables through PHP's POST superglobal array directly like so:
var_dump($_POST['first_name']);
and that returned empty as well so then I tried dumping the entire post array like so:
var_dump($_POST);
and it was empty as well despite the fact that I filled out the entire form. Nevertheless, the records in my MySQL database were being updated (which means that the form was submitting even though my $_POST variables appeared empty).
Also, I reasoned that normally, if I var dumped variables in the controller function before a redirect function call that it should give me a 'Headers already sent' error but it never did. It just redirected me to the supposed success page instead of dumping my variables.
So for the about 2 hours I thought that my POST data wasn't being sent and re-checked the code for errors and began commenting out statements one by one until I could find the culprit statement in my script.
Finally, I commented out a chunk of code that sets a success message an redirects, like so:
/*
if($record_updated_successfully)
{
$this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
*/
and only then did the script start dumping out all my previous variable dumps using codeigniter's $this->input->post function as well as the $_POST superglobal array.
So ok, if the script does indeed redirect me despite the variable dumps sending output before headers are sent then I can see why the $_POST variables would appear empty.
So then the real question is why the script would still redirect despite my sending of output before headers are sent? Has anyone ever experienced this?
Any help with this would be appreciated.
EDIT: with respect to loading the view here's a simplified version of my script looks like
with the debugging var dump statements:
function some_controller_method() {
var_dump($this->input->post());
var_dump($_POST);
// some code
if($this->input->post('form_action') == 'update record') {
// code that runs when the form is submitted
/*
* ...
*/
if($record_updated_successfully)
{
$this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
}
$this->load->view('my-view-file.php');
}
While I can't be sure, I'm going to assume you were outputting things like the var_dump() in your view file. A view is not executed at the time you call it, for example:
$this->load->view('some_view');
echo "hi!";
In a controller will not result in the contents of some view followed by "hi". It will results in "hi" followed by the contents of some view. The view is actually output after everything else in the controller has run.
This is the only thing that comes to mind with the information you've presented. I'd have to see more code to offer a different diagnosis.
I had "the same" problem and I found an unset() function in a loop in my code. Perhaps this will help.

PHP question on how to display data?

How can I accomplish the following.
For example lets say I already have a template that checks to see if the user has entered a link if not it will not display the link template if so display the link template?
The basics are:
<?PHP
if(isset($_REQUEST['supplied_link']))
{
// do something
}
else
{
// do another thing
}
However, it is very important to actually validate that link in some manner, particularly to ensure that it is not script code but is, in fact, a link. I chose $_REQUEST because it handles both POST and GET, but you could use $_POST as meder described.
In terms of validating, if you're using PHP 5, you can just use strpos to look for http:// at the front. Remember, in this case, the return value would be 0 (zero) for the desired match and either FALSE or > 0 for a failure. You could do a lot more than this (such as validating the URL against DNS, Spam blockers, etc), but this is the bare minimum.
<?php
$enteredLink = isset( $_POST['enteredLink'] ) ?1:0;
if ( $enteredLink ) {
?>
link
<?php
}
?>
You need to set a variable to a boolean, use an if statement and if it was entered, just output anything, otherwise don't.

Passing data from one php page to another is not working - why?

Frustrated php novice here...
I'm trying to pass a "type" value of either billto or shipto from ab.php to abp.php.
ab.php snippet:
<?php
echo '<a href="' . tep_href_link(FILENAME_ABP, 'edit=' . $bill_address['address_book_id'] . '&type=billto', 'SSL') . '">' .
tep_image_button('small_edit.gif', SMALL_IMAGE_BUTTON_EDIT) .
'</a>';
?>
This does add the &type=billto to the end of the url. It looks like this:
www.mydomain.com/abp.php?edit=408&type=billto&id=4a6524d
abp.php snippet:
if ($HTTP_GET_VARS['type'] == 'billto') {
then it does a db update...
The if returns false though (from what I can tell) because the update is not performed.
I've also tried $_GET instead of $HTTP_GET_VARS.
Because the code in abp.php isn't executed until after the user clicks a button, I can't use echos to check the value, but I can see the type in the url, so I'm not sure why it's not executing.
Could really use some direction... whether it's what I need to change, or even just suggestions on how to troubleshoot it further. I'm in the middle of a huge learning curve right now. Thanks!!!
edit:
Sorry, I just realized I left out that after the db update the user goes back to ab.php. So the whole workflow is this:
User goes to ab.php.
User clicks link to go to abp.php.
User changes data on abp.php.
User clicks button on abp.php.
Update to db is executed and user is sent back to ab.php.
Because the code in abp.php isn't executed until after the user clicks a button, I can't use echos to check the valueWhy not?
echo '<pre>Debug: $_GET=', htmlspecialchars(var_export($_GET, true)), "</pre>\n";
echo '<pre>Debug: billto===$_GET[type] = ', 'billto'===$_GET['type'] ? 'true':'false', "</pre>\n";
if ( 'billto'===$_GET['type'] ) {
...
edit: You might also be interested in netbeans and its php module:
"Debug PHP code using Xdebug: You can inspect local variables, set watches, set breakpoints, and evaluate code live. Navigate to declarations, types and files using Go To shortcuts and hypertext links."
try something like this
if ($_GET['type'] == 'billto') {
die("got to here, type must == billto");
this will prove that your if statement is working or not,
it may be that the update part is not working
Before the if statement - try
var_dump($_GET);
And make sure the 'billto' is contained within the $_GET array. Of course, if you have got the debuger setup, you should be able to watch the value of the $_GET array
or try:
var_dump($_REQUEST);
Check the URL of the second page, is it in the correct form? From the code snippet you post, I don't know if there would be a ? after the question mark. Also try to disable the redirect to see if your code is working as it should.
One other thing is you may want to put the new url in a variable first, then put that into the link HTML. It's less efficient, but makes the code easier to read and debug.
Try turning on error reporting, place this at the start of your php script
error_reporting ( E_ALL | E_STRICT)
PHP is very tolerant of typos and accessing subscripts in an array that does not exist; by enforcing strict and reporting all errors you would be able to catch those cases.
If you can't see the output, try this:
function log_error($no,$msg,$file,$line)
{
$errorMessage = "Error no $no: $msg in $file at line number $line";
file_put_contents("errors_paypal.txt",$errorMessage."\n\n",FILE_APPEND);
}
set_error_handler('log_error');
You may have to set some file permissions for the log file to be written to.
Of course, you can get Netbeans and its debugging module too.

Categories