Php used in wordpress widget form method

//Backend Form
public function form( $instance ) {
    global $wp_customize;
    $title = isset( $instance['title'] ) ? $instance['title'] : '';
    $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';

    // Get menus
    $menus = wp_get_nav_menus();

    // If no menus exists, direct the user to go and create some.
    ?>
    <p class="nav-menu-widget-no-menus-message" <?php if ( ! empty( $menus ) ) { echo ' style="display:none" '; } ?>>
        <?php
        if ( $wp_customize instanceof WP_Customize_Manager ) {
            $url = 'javascript: wp.customize.panel( "nav_menus" ).focus();';
        } else {
            $url = admin_url( 'nav-menus.php' );
        }
        ?>
        <?php echo sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.' ), esc_attr( $url ) ); ?>
    </p>
    <div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ) ?></label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>"/>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
            <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
                <option value="0"><?php _e( '&mdash; Select &mdash;' ); ?></option>
                <?php foreach ( $menus as $menu ) : ?>
                    <option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $nav_menu, $menu->term_id ); ?>>
                        <?php echo esc_html( $menu->name ); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </p>
        <?php if ( $wp_customize instanceof WP_Customize_Manager ) : ?>
            <p class="edit-selected-nav-menu" style="<?php if ( ! $nav_menu ) { echo 'display: none;'; } ?>">
                <button type="button" class="button"><?php _e( 'Edit Menu' ) ?></button>
            </p>
        <?php endif; ?>
    </div>
    <?php
}

The above code is using the WordPress form method. I have a various question in it.

Question #1
<div class="nav-menu-widget-form-controls" <?php if ( empty( $menus ) ) { echo ' style="display:none" '; } ?>>

What does this mean? If there are no drop down menus set then do not display anything?

Question #2
<select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">

what kind of code writing is this → $this->
Also,

what is ‘nav_menu’ Is it some argument/variable that is predefined in the WordPress?
Is this some kind of array? please help me to understand this, and guide me. Thanks.

Yes, literally if the $menus variable is empty, then output a further CSS style to not display the div.

Can you please also help me with the other questions also that I have asked?

http://php.net/manual/en/sdo.sample.getset.php

Example #1 Access via property name

Data object properties can be accessed using the object property access syntax. The following sets the company name to ‘Acme’.

<?php
$company->name = 'Acme';
?>

http://php.net/manual/en/language.oop5.basic.php

Properties and methods

Class properties and methods live in separate “namespaces”, so it is possible to have a property and a method with the same name. Referring to both a property and a method has the same notation, and whether a property will be accessed or a method will be called, solely depends on the context, i.e. whether the usage is a variable access or a function call.

Example #7 Property access vs. method call

class Foo
{
    public $bar = 'property';
    
    public function bar() {
        return 'method';
    }
}

$obj = new Foo();
echo $obj->bar, PHP_EOL, $obj->bar(), PHP_EOL; 

So If I am not wrong then this piece of code→ $this->get_field_id( 'title' );, for example, fetches a database entry form the data.

In fact, it fetches the last updated database entry for the “title”. Is that correct?

Being WordPress, that’s a fairly safe bet. WordPress uses the database extensively, which is great for loading content dynamically into templates. It also means that consideration should be given to using it efficiently.

As for “last”, that would depend on what the “this” is.
As for what the returned “entry” is, because WordPress has a lot of functions, more than I can remember all the specifics for, I usually refer to the codex. In this case

Return
(string) ID attribute for $field_name.

Nope, no real idea on the rest, or at least not enough idea to provide an explanation that would as clear as those that others have provided. I just cherry-picked the one that was easy to answer. :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.