Filter Since 3.1.0 uncanny-learndash-groups

list_pages

Filters the page title when creating an HTML drop-down list of pages. Filters the page title when creating an HTML drop-down list of pages.

add_filter( 'list_pages', $callback, 10, 2 );

Description

Filters the page title within the `WP_Walker_Nav_Menu` or custom walkers when generating lists, particularly for navigation menus and dropdowns. This allows developers to modify or sanitize page titles before they are displayed. It's commonly used for adjusting output based on theme or plugin requirements.


Usage

add_filter( 'list_pages', 'your_function_name', 10, 2 );

Parameters

$title (string)
Page title.
$page (WP_Post)
Page data object.

Return Value

The filtered value.


Examples

/**
 * Example of using the 'list_pages' filter to modify page titles in a dropdown.
 * This example appends the page ID in parentheses to the page title if the page
 * is not the homepage.
 */
add_filter( 'list_pages', function( $title, $page ) {
	// Check if the current page is the homepage.
	// If it is, we don't want to append the ID.
	if ( ! is_front_page() && $page->ID !== get_option('page_for_posts') ) {
		// Append the page ID in parentheses to the title.
		$title .= ' (' . $page->ID . ')';
	}

	return $title;
}, 10, 2 ); // Priority 10, accepts 2 arguments ($title, $page).

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/includes/class-walker-group-dropdown.php:96
src/includes/class-walker-group-dropdown.php:417

public function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {

		$padding = apply_filters(
			'ulgm_group_mangement_group_dropdown_padding',
			[
				'input'      => '— ',
				'multiplier' => $depth * 1
			],
			$depth
		);

		$pad = str_repeat( $padding['input'], $padding['multiplier'] );

		if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
			$args['value_field'] = 'ID';
		}

		$output .= "t<option class="level-$depth" value="" . esc_attr( $page->{$args['value_field']} ) . '"';
		if ( $page->ID == $args['selected'] ) {
			$output .= ' selected="selected"';
		}
		$output .= '>';

		$title = $page->post_title;
		if ( '' === $title ) {
			/* translators: %d: ID of a post. */
			$title = sprintf( __( '#%d (no title)' ), $page->ID );
		}

		/**
		 * Filters the page title when creating an HTML drop-down list of pages.
		 *
		 * @param string  $title Page title.
		 * @param WP_Post $page  Page data object.
		 *
		 * @since 3.1.0
		 *
		 */
		$title = apply_filters( 'list_pages', $title, $page );

		$output .= $pad . esc_html( $title );
		$output .= "</option>n";
	}

Scroll to Top