Skip to content

Instantly share code, notes, and snippets.

@pixelbart
Last active August 11, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbart/43b91abb8cfbc2575bfd2390b14a1550 to your computer and use it in GitHub Desktop.
Save pixelbart/43b91abb8cfbc2575bfd2390b14a1550 to your computer and use it in GitHub Desktop.
Helpful Plugin: Anker zum Formular

Installation

Den angegeben Code in die functions.php deines WordPress Themes packen.

Anwendungsbeispiel

[helpful_anchor] an der gewünschten Stellen platzieren, wo dann der Link ausgegeben werden soll.

Um den Shortcode in PHP zu verwenden, genügt folgende Anwendung:

<?php echo do_shortcode( '[helpful_anchor]' ); ?>

Ausgabe:

<a href="LINK_ZUM_BEITRAG#helpful-ID_DES_BEITRAGS">Feedback geben</a>

Weitere Anwendungsbeispiele

<?php // functions.php
/**
* Fügt Helpful eine ID hinzu.
*
* @param string $content
*
* @return string
*/
add_filter( 'the_content', function( $content ) {
global $post;
if ( preg_match( "~class=\"helpful~", $content ) ) {
$id = 'helpful-' . $post->ID;
$pos = strpos( $content, 'class="helpful' );
if ( $pos !== false ) {
$content = substr_replace($content, 'id="' . $id . '" class="helpful', $pos, strlen( 'class="helpful' ) );
}
}
return $content;
}, PHP_INT_MAX);
/**
* Fügt einen neuen Shortcode [helpful_anchor] hinzu.
*
* @return string
*/
add_shortcode( 'helpful_anchor', function() {
global $post;
$id = 'helpful-' . $post->ID;
$permalink = get_the_permalink( $post ) . '#' . $id;
return sprintf( '<a href="%s">%s</a>', $permalink, 'Feedback geben' ); // Hier ggf. den Text anpassen
});
@pixelbart
Copy link
Author

pixelbart commented Aug 11, 2020

Ohne ID des Beitrags, den folgenden Inhalt in die functions.php einfügen:

<?php // functions.php

/**
 * Fügt Helpful eine ID hinzu.
 *
 * @param string $content
 *
 * @return string
 */
add_filter( 'the_content', function( $content ) {
	global $post;
	if ( preg_match( "~class=\"helpful~", $content ) ) {
		$id  = 'helpful';
		$pos = strpos( $content, 'class="helpful' );
		if ( $pos !== false ) {
			$content = substr_replace($content, 'id="' . $id . '" class="helpful', $pos, strlen( 'class="helpful' ) );
		}
	}
	return $content;
}, PHP_INT_MAX);

/**
 * Fügt einen neuen Shortcode [helpful_anchor] hinzu.
 *
 * @return string
 */
add_shortcode( 'helpful_anchor', function() {
	global $post;	
	$id = 'helpful';
	$permalink = get_the_permalink( $post ) . '#' . $id;
	return sprintf( '<a href="%s">%s</a>', $permalink, 'Feedback geben' ); // Hier ggf. den Text anpassen
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment