0% found this document useful (0 votes)
20 views3 pages

How Would You Refactor This Bit of Code To Be More Concise in Wordpress Context?

The document discusses ways to improve PHP code in WordPress contexts. It provides examples of code that could be refactored or improved and asks how each example could be improved. Must-use plugins are also briefly described as plugins that are always activated by default and cannot be deactivated from the dashboard.

Uploaded by

suganya r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

How Would You Refactor This Bit of Code To Be More Concise in Wordpress Context?

The document discusses ways to improve PHP code in WordPress contexts. It provides examples of code that could be refactored or improved and asks how each example could be improved. Must-use plugins are also briefly described as plugins that are always activated by default and cannot be deactivated from the dashboard.

Uploaded by

suganya r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

How would you refactor this bit of code to be more concise in


WordPress context?
<?php

//Assume $map looks like this:

/*

$map = array(

Array ( ‘name’ => 'Prashant', 'surname' => 'Baldha'),

array('name' => ‘Joe’, 'surname' => 'Miles'),

array('name' => 'David', 'surname' => 'Anderson'),

array('name' => 'Charles', 'surname' => 'Tatum'), );

*/

$names = array();

foreach ($map as $data=>$row) {

if ( $data == $name)

$names[] = $data['name'];

return $name

?>

2. How can you improve the below code in wordpress context?


<?php

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<input type="text" name="name" name=<?php echo $name?> />

<input type="text" name="email" name=<?php echo $email_id?> />

<input type="text" name="name" name=<?php echo $phone?> />


<input type="submit" name="submit" value="Submit">

</form>

?>

3.What are MU plugins? Why they are used? Please describe it in brief
 A must-use plugin (also called a ‘mu-plugin’) is a plugin that will always be activated by
default.
 mu-plugins were only available for WPMU.

 Moreover, a mu-plugin can’t be deactivated in the Plugins page of your WordPress


dashboard:
 the only way to deactivate a mu-plugin is by deleting or moving its file.
 That way, no one can accidentally deactivate a needed feature.

 A mu-plugin is similar to a classic plugin, except that it is not stored in the plugins
directory of the wp-content folder,
 but rather in the mu-plugins directory, of the same folder. If you have never used mu-
plugins before,
 this directory should not exist, but you can create it without any problem.

4.How would you improve this code?


<?php

function my_option_code() {

update_option('google_site_key' , $_POST['google_site_key']);

add_action('option_code', 'my_option_code');

5.How would you improve this code?

<?phpglobal $wpdb;

$query = "SELECT option_name FROM $wpdb->options WHERE


option_value= '" . $my_option_name . "'";

$option_val = $wpdp->get_var($query);

$wpdb->execute();

$result = $wpdb->get_result();

You might also like