WordPress.org

Make WordPress Core

Opened 11 months ago

Last modified 11 months ago

#45861 new defect (bug)

Global variable in plugin main file

Reported by: rilwis Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Plugins Keywords: dev-feedback has-patch
Focuses: Cc:
PR Number:

Description

In the main plugin file, if we declare a variable, it will be used as a global variable in WordPress.

This might lead to conflict for global variable names.

The code is in wp-settings.php file.

A simple fix is creating a function to load a plugin and include main plugin file inside that function. So the scope is function scope, not global scope.

Attachments (1)

45861.patch (1.5 KB) - added by rilwis 11 months ago.
Proposed patch

Download all attachments as: .zip

Change History (5)

#1 @mukesh27
11 months ago

  • Keywords needs-patch dev-feedback added

@rilwis
11 months ago

Proposed patch

#2 @mukesh27
11 months ago

@rilwis Just create new function to include plugin file is solve the issue you have created? also you have added @return void in your patch 45861.patch​ that do not needed as function does not return any return value.

Needs to wait for code developer reply on this ticket

#3 @mukesh27
11 months ago

  • Keywords has-patch added; needs-patch removed

#4 @rilwis
11 months ago

@mukesh27 Yes, using a function to include plugin files makes the variables available in the function scope only, which means they're not available in the global scope.

Using @return void is a valid (it's recommended but not a must) since it's the recommended way to tell developers that the function does not return anything.

Note: See TracTickets for help on using tickets.