Home » Beispiele & Scripte » Meta-Tags und SEO-Texte für bestimmte URL-Pfade hinterlegen

Meta-Tags und SEO-Texte für bestimmte URL-Pfade hinterlegen

OBS Hilfecenter
Inhalt

Um Meta-Tags und SEO-Texte für verschiedene Produkt- und Kategorie-Seiten zu hinterlegen können die Hooks in dem OBS Pages Plugin verwenden werden. In diesem Beispiel zeigen wir Ihnen wie Sie über ihr OceanWP Child Theme eine JSON Datei hinterlegen können um verschiedene Werte auf Ihren Produkt- und Kategorie-Seiten auszugeben.

PHP Funktion zum auslesen der JSON Datei

Die folgende PHP Funktion zum Auslesen der JSON Datei muss ins Child-Theme in die functions.php kopiert werden.

/**
 * Get the SEO text from a JSON file for a given path and position
 *
 * @param string $position The position of the requested SEO text.
 * @param string $path     The path of the requested SEO text.
 *
 * @return string|null The SEO text if found, null otherwise.
 */
function obs_get_seo_from_json( string $position, string $path ): ?string {
	// SEO JSON file in (child) theme in assets folder.
	$path_to_seo_json = get_stylesheet_directory() . '/assets/seo-values.json';
	// check if file exists.
	if ( file_exists( $path_to_seo_json ) ) {
		$json_seo = file_get_contents( $path_to_seo_json );
		if ( false !== $json_seo ) {
			$seo_array = json_decode( $json_seo, true );
			// make sure the file has valid JSON content.
			if ( null !== $seo_array ) {
				// check for the given path and position.
				if ( array_key_exists( $path, $seo_array ) ) {
					return $seo_array[ $path ][ $position ];
				}
			}
		}
	}

	return null;
}

Aufbau der JSON Datei

Das folgende Beispiel zeigt die Struktur der JSON Datei um die entsprechenden Einträge bei einer Shop-URL anzupassen. Die JSON Datei muss im Child-Theme Ordner /assets/seo-values.json vorliegen. Teile, die beim Aufruf der Shop-URL nicht ersetzt werden sollen, können einfach in der JSON weggelassen werden. Denken Sie an die utf-8 Codierung. HTML Inhalte müssen escaped werden.

Dateiaufbau:

Am Anfang ist immer die eindeutige URL der Seite eingetragen, für die HTML Teile ersetzt oder hinzugefügt werden sollen. Unterhalb können die folgenden Feldnamen definiert werden:

title = Meta Titel der Seite
description = Meta Seitenbeschreibung
text-before-content = Inhalt vor dem Content der Seite eintragen
text-after-content = Inhalt nach dem Content der Seite eintragen

{
  "/shop/wg/bekleidung/helme/t/marke-ktm/": {
    "title": "SEO Title für Helme der Marke KTM",
    "description": "Jetzt kommt ein langer SEO Text für die Description von tollen Helmen",
    "text-before-content": "<h2>Title before Content</h2>\nGanz tolle Helme exklusiv bei uns",
    "text-after-content": "<h2>Überschrift am Ende</h2>\nGanz tolle Helme exklusiv bei uns"
  },
  "/shop/wg/bekleidung/": {
    "title": "SEO Title Bekleidung",
    "description": "SEO Description Bekleidung",
    "text-after-content": "<h2>Überschrift am Ende</h2>\nGanz tolle Helme exklusiv bei uns"
  },
  "/product/husqvarna-document-folder/": {
    "title": "SEO Title Dokumente Folder",
    "text-before-content": "<h2>Title before Content</h2>\nGanz tolle Helme exklusiv bei uns"
  }
}

PHP Funktion zum Anpassen des Meta Title

Der Meta Title kann mit folgender Funktion in der functions.php angepasst werden. Der Filter obs_meta_title ist ab OBS Pages Version 1.7.3 verfügbar. Nur wenn Sie Meta Title über die JSON Datei ausgeben möchten, muss die folgende PHP Funktion ins Child-Theme in die functions.php kopiert werden.

/**
 * Adjust the SEO title of certain product or category page, if specified in the JSON file
 *
 * @param string $title        The already set title.
 * @param string $path         The path of the current OBS page.
 * @param array  $explore_path The explore_path array holding lots of info about the current page.
 *
 * @return string The new to set title.
 */
function obs_adjust_seo_title( string $title, string $path, array $explore_path ): string {
	// check if there is a title in the JSON file.
	$seo_title = obs_get_seo_from_json( 'title', $path );
	if ( null !== $seo_title ) {
		$title = $seo_title;
	}

	return $title;
}

add_filter( 'obs_meta_title', 'obs_adjust_seo_title', 10, 3 );

PHP Funktion zum Anpassen der Meta Description

Die Meta Description kann mit folgender Funktion in der functions.php angepasst werden. Der Filter obs_meta_description ist ab OBS Pages Version 1.7.3 verfügbar. Nur wenn Sie Meta Description über die JSON Datei ausgeben möchten, muss die folgende PHP Funktion ins Child-Theme in die functions.php kopiert werden.

/**
 * Adjust the SEO description of certain product or category page, if specified in the JSON file
 *
 * @param string $description  The already set description.
 * @param string $path         The path of the current OBS page.
 * @param array  $explore_path The explore_path array holding lots of info about the current page.
 *
 * @return string The new to set description.
 */
function obs_adjust_seo_description( string $description, string $path, array $explore_path ): string {
	// check if there is a description in the JSON file.
	$seo_description = obs_get_seo_from_json( 'description', $path );
	if ( null !== $seo_description ) {
		$description = $seo_description;
	}

	return $description;
}

add_filter( 'obs_meta_description', 'obs_adjust_seo_description', 10, 3 );

SEO Texte am Anfang oder Ende der Seite

Viele WordPress Themes bieten eine Vielzahl an Hooks, die vom Child Theme aus genutzt werden können, um das Theme zu erweitern, zum Beispiel um Texte am Anfang oder Ende einer Seite anzuzeigen. Das OceanWP Theme, dass OBS als Standard nutzt, bietet verschiedene Möglichkeiten zum Einhängen.

PHP Funktion zum Ausgeben von SEO Texten am Anfang einer Seite

SEO Texte am Anfang einer Seite können mit folgender Funktion in der functions.php angepasst werden. Der Inhalt des Feldes text-before-content in der JSON Datei wird über den Filter ocean_before_content_inner ausgegeben. Nur wenn Sie SEO Texte am Anfang der Seite über die JSON Datei ausgeben möchten, muss die folgende PHP Funktion ins Child-Theme in die functions.php kopiert werden.

/**
 * Add some HTML to the start of a page, if specified in the JSON file
 */
function obs_oceanwp_before_content_inner() {
	global $wp;
	if ( isset( $wp->oneboxshop_page ) && method_exists( 'Oneboxshop_Pages_Functions', 'get_url_path' ) && is_callable( array( 'Oneboxshop_Pages_Functions', 'get_url_path' ) ) ) {
		$path = Oneboxshop_Pages_Functions::get_url_path();
		// check if there is a text in the JSON file.
		$seo_before_content = obs_get_seo_from_json( 'text-before-content', $path );
		if ( null !== $seo_before_content ) {
			echo $seo_before_content;
		}
	}
}

add_filter( 'ocean_before_content_inner', 'obs_oceanwp_before_content_inner' );

PHP Funktion zum Ausgeben von SEO Texten am Ende der Seite

SEO Texte am Ende einer Seite können mit folgender Funktion in der functions.php angepasst werden. Der Inhalt des Feldes text-after-content in der JSON Datei wird über den Filter ocean_after_content_inner ausgegeben. Nur wenn Sie SEO Texte am Ende der Seite über die JSON Datei ausgeben möchten, muss die folgende PHP Funktion ins Child-Theme in die functions.php kopiert werden.

/**
 * Add some HTML to the end of a page, if specified in the JSON file
 */
function obs_oceanwp_after_content_inner() {
	global $wp;
	if ( isset( $wp->oneboxshop_page ) && method_exists( 'Oneboxshop_Pages_Functions', 'get_url_path' ) && is_callable( array( 'Oneboxshop_Pages_Functions', 'get_url_path' ) ) ) {
		$path = Oneboxshop_Pages_Functions::get_url_path();
		// check if there is a text in the JSON file.
		$seo_after_content = obs_get_seo_from_json( 'text-after-content', $path );
		if ( null !== $seo_after_content ) {
			echo $seo_after_content;
		}
	}
}

add_filter( 'ocean_after_content_inner', 'obs_oceanwp_after_content_inner' );

Hilfecenter: