wordpress (301 redirect plugin)/ php question - php

I am setting up a website in wordpress. the site was done by someone else. After setting up the site / database I get the following error:
Parse error: syntax error, unexpected $end in C:\Documents and Settings\user101\
Desktop\wordpress\wp-content\plugins\wp-301redirect\wp-301redirect.php on
line 320
line 320 is the very last line of wp-301redirect.php page. I looked up what 301redirect is and it is apparently a plugin. The wp-301redirect.php page I have is exactly what the plugin provides (zip download). line by line.
Does anyone know what might be causing this.?

In the default file for this plugin. line 220 has <? it should be changed to <?php to fix this error

Try removing the last line after ?>. And generally it's advisable to not close your PHP with ?> as it's not required and prevents this from happening in the first place.

Most likely, a function or control block is missing its matching end-brace, such as:
function foo() {
//stuff
...
// EOF
OR
if (true) {
...
// EOF

Related

Laravel - syntax error, unexpected end of file

I have a website which works fine on host, but I'm currently trying to install it on localhost.
I've downloaded everything and configured to work on localhost - Database & URL.
The problem is this error:
Unhandled Exception
Message:
syntax error, unexpected end of file Location:
C:\Program Files (x86)\EasyPHP-12.1\www\laravel\view.php(386) :
eval()'d code on line 118
And I don't know what causes it. Any solutions?
P.S. I've setup in my windows' host file 127.0.0.1 myproject.dev.
There is an error within one of your views. If there is a more detailed stack trace it should show you details of a view, although the name will be an md5() string so it's a bit hard to find. You might want to delete all compiled Blade views in storage/views and let Blade re-compile the views.
If you still get the error then check your views to make sure you have all the proper closing tags, e.g., #endif or #endforeach
Always double check your views for any syntax errors.
I've run into this same error and I was able to fix it by adding spaces to the content within an inline if statement. For example:
Experienced error with:
#if( BLAH )Output#endif
Fixed error with:
#if( BLAH ) Output #endif
This may not be a problem in all cases and it was certainly difficult to track down but it is just one example that can cause this exact error.
A variation of this problem -- I had a php block, which I'd opened with
<? as opposed to <?php worked fine on LocalHost/MAMP, but gave the above error under Nginx/Ubuntu 16.04/PHP7 (both Laravel)
you should remove a character from view file. for example my character was "," (a comma) before some "#endfor". when i remove those worked!

WordPress plugin activation error: unexpected output during activation [duplicate]

I'm getting this message each time I activate my plugin:
The plugin generated 80 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
The only way I was able to suppress the message was to wrap my activation function code within an if statement (please refer to snippets below).
Here, a snippet of my plugin code when I get the error described above:
function myPlugin( $post ) {
echo "Whatever is here throws an unexpected output alert when the plugin isa activated";
}
register_activation_hook( __FILE__, 'myPlugin' );
Following, my wrapping the function in my plugin within an if statement; it suppresses the previous error as discussed above:
function myPlugin( $post ) {
global $pagenow;
if ( is_admin() && $pagenow !== 'plugins.php' ) {
echo "No more alerts when its wrapped this way";
}
}
}
register_activation_hook( __FILE__, 'myPlugin' );
What actually cause that error and how can I effectively complete my plugin with my logics without having to encounter it?
Is there any better way to handle this?
2 probably reasons:
1) You are doing an output (like echo or etc) in wrong place.
Do you want to output a message in admin dashboard? - use admin_notices hook and output there...
Do you want to output a message in front-end? - find appropriate places with hooks (like the_content or wp_footer or whatever).
Don't output anything either in register_activation_hook or outside of WordPress standard hooks, no-one should do that.**
2) if you aren't doing any output intentionally, then maybe some php error happens? If so, put this code temporarily in functions.php and then activate the plugin - you will see the error.
define('temp_file', ABSPATH.'/_temp_out.txt' );
add_action("activated_plugin", "activation_handler1");
function activation_handler1(){
$cont = ob_get_contents();
if(!empty($cont)) file_put_contents(temp_file, $cont );
}
add_action( "pre_current_active_plugins", "pre_output1" );
function pre_output1($action){
if(is_admin() && file_exists(temp_file))
{
$cont= file_get_contents(temp_file);
if(!empty($cont))
{
echo '<div class="error"> Error Message:' . $cont . '</div>';
#unlink(temp_file);
}
}
}
Had the same error, but only with 6 characters )
so... in my case I had empty lines after PHP closing tag ?> - that will cause this error too.
I think there may be two issues here that are causing the problem. First is that I don't think wordpress expects any output when the plugin activation hook is called so it may be complaining about that. Second is that plugin activation hooks are called fairly early in the wordpress program flow, so, it's probably being called before headers are sent. If ANY output is generated before calling header() then PHP usually complains.
Usually the plugin activation routine is reserved for basic setup of the plugin, calls to things like set_option() and the like.
I had the same error - 3 characters of unexpected output and was lead here. For people in my scenario another cause of this message can be the file type being encoded as UTF with BOM.
BOM encoding was causing the error, and while the plug-in activated it would render incorrectly in internet explorer because of this.
The solution is to use Notepad++ and choose 'Convert to UTF without BOM', or if you are using visual studio, there is an explanation of how to change encoding UTF-8 without BOM
I battled this problem for a long time. Typically this is caused by spaces or new lines before the opening <?php tag or after the closing ?> tag. Once I removed these, the error went away.
Also, never assume anything about GET, POST, COOKIE and REQUEST variables. Always check first using isset() or empty().
In my case it was due to Undefined index, Just enable the debug log to check whats causing it, then you can resolve it easily.
For those who don't know how to enable the Debug log, Add these lines in your wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'WP_DEBUG_LOG', true );
You can see the errors properly in the debug file created in wp-content
sometime it is because you use <?php ;?> unnecessary or use it like shown below
;?>
<?php
this extra line between closing and starting tag may also cause this error, simple remove that line/space
A common way to assign the register_activation_hook is using a static method of a class. This ensures that your plugin activation function name won't collide with other plugins.
class Foo_Plugin
{
public static function plugin_activation() {
// activation logic
}
}
This function needs to be public and not private. A mistake is easily made though, so this could be a reason for your problems when getting this kind of error.
You would then register the activation with this code in the main plugin file.
register_activation_hook( __FILE__, array( 'Foo_Plugin', 'plugin_activation' ) );
The error message The plugin generated *X* characters of unexpected output during activation is not very helpful or at least not quite enough.
To help locate the issue, add these to wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
Then check wp-content/debug.log for more detailed error messages as to the origin of the error.
I had the same problem. I noticed that there were new lines at the beginning of the file. So I removed them and the errors disappeared. Try removing the new lines at the beginning of your files. That may help.
For beginner level developer its must be empty line break after "?>" closing of php tags.
Try removing all empty line break after this.
This problem can be solved by removing extra whitespaces. I solved this problem for my code. You can remove extra whitespaces easily in Adove Dreamweaver.
First, goto edit->Find and Replace. Or press Ctrl+F. Check "Use Regular Expression" button from "option" section.
Fill "find" field with the below code
[\r\n]{2,}
Fill "Replace" field with the below code
\n
Now click on "Replace All" button.
Hope It will work.
I was having the same issue, i tried to remove the code and did everything but still the same problem
the real solution for me was the following.
at the end of the file which contains the header of the plugin i removed closing php ?> and the problem will be solved
at the end of the plugin file which contains the header of the plugin remove the extra line breaks after the closing php ?>
my plugin file ended at line 69 and there were 7 more spaced after the last php code so in the editor the file was up to 66, my error was "The plugin generated 7 characters of unexpected...." when i removed extra lines up to 69 the was error gone,
Thanks
First sight checklist for unexpected output error on plugin.
Remove closing php tag ?> in all php file of plugin.
Remove empty space before php tag <?php
Avoid echo content in function/hook otherwise clean buffer when needed.
check here to see more info you can use:
<?php
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
If (is_plugin_active('wshops/init.php'))
{
//Run your plugin includes files or functions
}
in your init php file.
i also got this problem when i activate my plugin
The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Typically this is caused by spaces or new lines before the opening tag. Once I removed these, the error went away.
now my plugin error gone.
My problem was that in the main php file, i had appended at the end of the file a javascript function. It seems that wordpress hooks such of a function in the head element. I externalised that function into a java script file.
Before:
<?php
/**
* Plugin Name: yyy
* Description: yyy
* Author: yyy
* Author URI: yyy
* Version: yyy
*/
/* many functions here */
function insert_in_header() {
echo '<script type="text/javascript">',
"perform_redirection(", '"', get_option('root'), '"', ", ", '"', get_option('redirect_to'), '");',
'</script>';
}
add_action('wp_head', 'insert_in_header');
?>
<--! PROBLEMS HERE-->
<script type = "text/javascript">
function perform_redirections(root, redirectionLink) {
// code here
}
</script>
After:
<?php
/**
* Plugin Name: yyy
* Description: yyy
* Author: yyy
* Author URI: yyy
* Version: yyy
*/
/* many functions here */
function insert_in_header() {
// in headscripts.js i put the perform_redirection function
echo '<script type="text/javascript" src="', plugins_url('js/headscripts.js', __FILE__ ), '"> </script>';
echo '<script type="text/javascript">',
"perform_redirection(", '"', get_option('root'), '"', ", ", '"', get_option('redirect_to'), '");',
'</script>';
}
add_action('wp_head', 'insert_in_header');
?>
For me there was some kind of error which was being swallowed and not caught during debug
If ound this article here which explains how to determin what this output actually is
$unexpectedOutput= ob_get_contents();
now you have it, you can echo it or inspect it during debug to find out what the hells going wrong, for me it was a prolem with a database script.
Credit to the article below
https://www.toddlahman.com/the-plugin-generated-x-characters-of-unexpected-output-during-activation/
just make the function static which your are calling on activation hook
Opening the PHP file in Notepad and saving it with ANSI encoding did the trick for me. It did not cause the issue to happen again even when I opened and saved it later in Visual Studio Code.
Thanks to Todd
After trying every answer and still coming short, I figured out my issue which was different to all answers here.
Basically, the unexpected characters were coming from my error log on the MAMP server. The errors weren't displaying on frontend or Wordpress installation error log, even with WP_DEBUG, WP_DEBUG_DISPLAY and display_errors = On in the php.ini
I eventually knew it was an error causing the umm error, so I dug a bit deeper and found some errors in my php_error.log which is located in MAMP/logs/php/php_error.log
Fixed the errors and the message from wordpress went away on activation.
I had the same problem and I just changed the private function to static and it's solved.
private function db_setup() { ....
to
static function db_setup() { ....
I hope this be helpful...
In my case I had added extra blank space before start <?php tag, what a wired error though
In my case, i forget to delete var_dump inside my __construct(). I just delete it and the warn not appear anymore.
I had this same issue. In my case, I was creating some tables when this error occurred. After further digging, I realized that I had this error happening when activating my plugin which then creates the tables.
Error Code: 1060. Duplicate column name 'stadium_id'
A simple mistake as you can see. However, because it threw an error, it caused the same header output error (“headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.) to display on the plugin page after activating.
Final thoughts, ensure that your code is not triggering any errors as this will and can cause the issue! Also, do not echo a message when using the register_activation_hook as already mentioned by T.Todua. Instead, use the admin_notices hook. https://developer.wordpress.org/reference/hooks/admin_notices/

code for a PHP form that connects with a database

I am having problems with creating a PHP form for a web site that popluates a MySQL database.
Here is the error message I am getting at the following URL: http://www.laboro.biz/employer.php:
Parse error: syntax error, unexpected $end in /hermes/bosoraweb013/b1108/ywh.tsarge83/laboro/employer-FormToEmail.php on line 228
The other file that is the action file is http://www.laboro.biz/employer-FormToEmail.php.
I need help with scripting to make this work.
Can anyone please help?
You're missing a curly brace somewhere in your script.
You're missing a { or have one more } or ; than needed. Look it over.

Call to undefined function from another php file

Alright this is what my code looks like
index.php
require_once($WebsiteRoot . "/include/testfile.php");
TestFunction();
/include/testfile.php
function TestFunction()
{
echo "It Works";
}
And it gives me the error:
Fatal error:
Call to undefined function TestFunction() in /path/index.php on line 49
Any idea what i'm doing wrong?
Thanks
You haven't included a <?php tag in the included file, so it's just interpreted as plaintext input.
Remember... there's no such thing as a PHP script. There's only files which contain PHP code blocks. Without at least one <?php opening tag, the PHP interpreter will never be invoked and the file's contents will simply be treated as output.
try calling another function from testfile.php, if this is'nt working, its something with the include. Add the code:
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
to the top of index.php and refresh the browser to see your errors, try debugging from there.
The problem that i can forsee is that you are using a URL instead of a path, your $websiteRoot variable should contain a path like:
$websiteRoot = "/var/www/html/websiteName";
OR
$websiteRoot = "C://xampp/htdocs/websiteName";
instead of a URL like:
$websiteRoot = "http://www.somesite.com";
I had a similar issue. I dug into the PHP in the included file and found an invalid PHP tag. I had <? instead of <?php. PHP 7.2 and earlier forgave that, but PHP 7.3 was throwing that same error you faced.
Make sure you're including the file you think you are. If your index.php page looks exactly like you've stated, then it won't return anything.
If you want to link to the same location from anywhere on the site without worrying about relative locations, then at the beginning of the file, put:
$WebsiteRoot=$_SERVER['DOCUMENT_ROOT'];
And it should work fine, provided your file would be located at http://mywebsite.com/include/testfile.php
Try renaming the included file.
I had an included file with the name "system.php". It looked as if the include command was just skipped. Even with the most strict error reporting there was no message and even an echo command in the main body of the included file did not produce output. It had worked ok under PHP 5 but after the upgrade to a 7.2 environment these problems arose. After much effort - I forgot how - I managed to get an error message. It said there was a conflict with a PEAR class with the name "system". Yet my file didn't contain any class, just variables and functions. Anyway, giving the file another name than "system.php" worked for me.
I hope someone else can add a more technical comment on what was going wrong here.

Syntax error on closing brace after opening a php tag

i'm getting trouble with a function that looks pretty much like this:
<?php
function my_function() {
if(!empty($variable)) {
//Do some stuff
}
else {
?>show a message<?php
}
}
?>
The problem is i'm getting a parse error:
Parse error: syntax error, unexpected ‘}’ in /www/bla/bla/bla.php on line 8
I know for a fact that i'm not missing or have an extra '}' brace because the code works just fine on my local server and i've run a validator to make sure the syntax is correct, however, when i port the code to the online server, i get the error.
I believe it has something to do with the php installation not supporting the closing and reopening of php tags between the condition but i'm not sure how would i go about fixing it.
I know i could just do echo 'message'; instead, but this is not the only place where the script uses that kind of syntax to display messages, so fixing it here would just mean i'd get the error on another line, and then another.
Thanks.
As it stands that piece of code runs just fine as is on PHP 5.2.14.
When you pasted in the code are you sure you pasted this line exactly as-is?:
?>show a message<?php
The only thing I can think of is that the code on the server is using a short open tag <? but SHORT_OPEN_TAG is turned off in the server's php.ini, for example :
?>show a message<?
I had a similiar issue with my file. This was throwing an error in my file:
<?php } ?>
This may seem silly, I added a comment which seemed to fix the error:
<?php
//
}
?>

Categories