Wp php text widget

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Allows PHP to be used within Text widgets.

License

manovotny/wordpress-php-text-widget

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Allows PHP to be used within Text widgets.

Simply activate the plugin to use PHP in Text widgets.

There are two ways to install WordPress plugins: via the WordPress Dashboard (recommended) and via FTP.

Installing via the WordPress Dashboard is recommended because this process makes sure all the file and folder permissions are set correctly.

Using the WordPress Dashboard

  1. Download the plugins.
  2. Unzip the `eightbit-plugins.zip«` archive on your computer.
  3. Locate the standard-php-text-widget directory within the eightbit-plugins directory.
  4. Rezip the standard-php-text-widget directory.
  5. Locate the Plugins menu.
  6. In the Plugins menu, click on Add New.
  7. On the Add New page, click on the Upload link at the top of the page.
  8. On the Upload page, locate the standard-php-text-widget.zip archive (zip) on your computer (created in Step #4) and click Install Now.
  9. On the successful upload page, click Activate.
  1. Download the plugins.
  2. Unzip the eightbit-plugins.zip archive on your computer.
  3. Locate the standard-php-text-widget directory within the eightbit-plugins directory.
  4. Connect to your server via FTP.
  5. Upload the standard-php-text-widget directory to the /wp-content/plugins/ directory on your server.
  6. In the WordPress Dashboard, navigate to the Installed Plugins page under the Plugins menu.
  7. Locate Standard PHP Text Widget and click Activate.

Works with Standard 3.0 — 3.2.

Источник

Wp php text widget

Sample Editable Widget

A widget as it appars to a site visitor.

HTML output for this widget looks like this:

 

This is a text widget

I can put HTML in here. Search me!

Each widget has its own way of outputting HTML that is relevant to the data being displayed. The wrapper tags for the widget are defined by the widget area in which it is being displayed.

The PHP code necessary to create a widget like the built-in text widget looks like this:

); > public $args = array( 'before_title' => '

', 'after_title' => '

', 'before_widget' => '
', 'after_widget' => '
', ); public function widget( $args, $instance ) < echo $args['before_widget']; if ( ! empty( $instance['title'] ) ) < echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; >echo '
'; echo esc_html__( $instance['text'], 'text_domain' ); echo '
'; echo $args['after_widget']; > public function form( $instance ) < $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'text_domain' ); $text = ! empty( $instance['text'] ) ? $instance['text'] : esc_html__( '', 'text_domain' ); ?>

get_field_id( 'title' ) ); ?>"> get_field_name( 'title' ) ); ?>" type="text" value="">

get_field_id( 'Text' ) ); ?>"> get_field_name( 'text' ) ); ?>" type="text" cols="30" rows="10">

public function update( $new_instance, $old_instance ) < $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; $instance['text'] = ( ! empty( $new_instance['text'] ) ) ? $new_instance['text'] : ''; return $instance; >> $my_widget = new My_Widget();

The code above will be explained in detail later in the article.

Developing Widgets

To create and display a widget, you need to do the following:

  1. Create your widget’s class by extending the standard WP_Widget class and some of its functions.
  2. Register your widget so that it’s made available in the Widgets screen.
  3. Make sure that your theme has at least one widget area in which to add the widgets.

Your Widget Class

 public function widget( $args, $instance ) < // outputs the content of the widget >public function form( $instance ) < // outputs the options form in the admin >public function update( $new_instance, $old_instance ) < // processes widget options to be saved >>

The documentation for each of these functions can be found in the widget class code:

  1. construct : Set up your widget with a description, name, and display width in your admin.
  2. widget : Process the widget options and display the HTML on your page. The $args parameter provides the HTML you can use to display the widget title class and widget content class.
  3. form : Display the form that will be used to set the options for your widget. If your widget doesn’t have any options, you can skip this function (although it is still best practice to include it even if it’s empty).
  4. update : Save the widget options to the database. If your widget doesn’t have any options, you can skip this function (although it is still best practice to include it even if it’s empty).

Registering a Widget

The register_widget() function is used to register a widget.

Call this function using the widgets_init hook:

Examples

Example Text Widget

To build the text widget from the example at the beginning of this article. You will start by setting up a widget class that extends the WP_Widget class.

In the class constructor you will call the parent constructor and pass it your widget’s base ID and name. Also in the class constructor you will hook into the widgets_init action to register your widget.

Next you will declare the arguments you will use when creating your widget. There are four arguments you must define, before_title , after_title , before_widget , and after_widget . These arguments will define the code that wraps your widgets title and the widget itself.

After defining your arguments, you will define the widgets function. This function takes two parameters, the $args array from before, and the $instance of the widget, and is the function that will process options from the form and display the HTML for the widget on the front-end of your site. In the example above the widget function simply outputs the widget title, while passing it through the widget_title filter. It then out puts a simple widget wrapper and the content of the widget’s text field. As outlined in the example, you can access the options from the widget that are stored in the $instance .

Next you will define the form function. This function takes one parameter, $instance , and outputs the form that the user uses to create the widget in the Widgets admin screen. In the example above, the function starts by defining the $title and $text variables and setting them to the previously entered values, if those values exist. Then it outputs a simple form with a text field for the title and a textarea for the text content.

Lastly you will define the update function. This function takes two parameters, $new_instance and $old_instance , and is responsible for updating your widgets with new options when they are submitted. Here you simply define $instance as an empty array. You then set the title and text keys to the $new_instance values if they exist. You then return $instance .

Finally, when all of the above is defined, you instantiate your new widget class and test your work.

Sample Widget

 __( 'A Foo Widget', 'text_domain' ) ) // Args ); > /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) < extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); echo $before_widget; if ( ! empty( $title ) ) < echo $before_title . $title . $after_title; >echo __( 'Hello, World!', 'text_domain' ); echo $after_widget; > /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) < if ( isset( $instance['title'] ) ) < $title = $instance['title']; >else < $title = __( 'New title', 'text_domain' ); >?> 

get_field_name( 'title' ); ?>"> get_field_name( 'title' ); ?>" type="text" value="" />

/** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) < $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; return $instance; >> // class Foo_Widget

This sample widget can then be registered in the widgets_init hook:

Example with a Namespace

If you use PHP 5.3 with namespaces you should call the constructor directly as in the following example:

and call the register widget with:

Special considerations

If you want to use a widget inside another template file, rather than in a sidebar, you can use the_widget() to display it programmatically. The function accepts widget class names. You pass the widget class name to the function like this:

You may want to use this approach if you need to use a widget in a specific area on a page, such as displaying a list of events next to a form in a section on the front page of your site or displaying an email capture form on a mega-menu alongside your navigation.

Источник

Читайте также:  Pip понизить версию python
Оцените статью