Last active
October 22, 2022 08:50
-
-
Save anwas/6bdbabafa1e3fa85d8433ddd0ccd557e to your computer and use it in GitHub Desktop.
[Facebook OpenGraph integracija WordPress temoje]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Adding the Open Graph in the Language Attributes | |
| * @param $output | |
| * | |
| * @return string | |
| */ | |
| function mysite_add_opengraph_doctype( $output ) { | |
| return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"'; | |
| } | |
| add_filter( 'language_attributes', 'mysite_add_opengraph_doctype' ); | |
| /** | |
| * Lets add Open Graph Meta Info | |
| */ | |
| function my_site_insert_fb_in_head() { | |
| $custom_image_id = ''; | |
| // naudojant ACF sukurtą temų nustatymų puslapį įkelti nuotrauką ar paveikslėlį | |
| // turi būti nustatytas grąžinti image ID | |
| // jei tokio option įrašo nėra, priskiriama tuščia eilutė | |
| $custom_image_id = get_option( 'site_fb_image', '' ); // image ID | |
| if ( empty( $custom_image_id ) ) { | |
| // galima įkelti į svetainę per administravimą ir iš ten paimti image ID | |
| $custom_image_id = null; // TODO: vietoj null įrašyti image ID | |
| } | |
| if ( is_front_page() ) { | |
| ?> | |
| <meta property="og:title" content="<?php esc_attr( bloginfo( 'name' ) ); ?>" /> | |
| <meta property="og:site_name" content="<?php esc_attr( bloginfo( 'name' ) ); ?>" /> | |
| <meta property="og:url" content="<?php esc_url( bloginfo( 'url' ) ); ?>" /> | |
| <?php | |
| $description = ''; | |
| // naudojant ACF sukurtą temų nustatymų puslapį įdėti aprašymą | |
| // jei tokio option įrašo nėra, priskiriama tuščia eilutė | |
| $description = get_option( 'site_slogan', '' ); // iš ACF temų nustatymo puslapio | |
| if ( empty( $description ) ) { | |
| $description = get_bloginfo( 'description' ); | |
| } | |
| ?> | |
| <meta property="og:description" content="<?php echo esc_attr( $description ); ?>" /> | |
| <meta property="og:type" content="website" /> | |
| <?php | |
| if ( is_null( $custom_image_id ) || empty( $custom_image_id ) ) { | |
| // TODO: svetainės temos aplanke jau turi būti toks failas | |
| $file_path = '/assets/images/default-fb-cover.png'; | |
| if ( file_exists( get_stylesheet_directory() . $file_path ) ) { | |
| $image[0] = get_stylesheet_directory_uri() . $file_path; | |
| } else { | |
| $image[0] = false; | |
| } | |
| } else { | |
| $image = wp_get_attachment_image_src( $custom_image_id, 'full' ); | |
| } | |
| if ( is_array( $image ) && false !== $image[0] ) { | |
| ?> | |
| <meta property="og:image" content="<?php echo esc_url( $image[0] ); ?>"/> | |
| <?php | |
| } | |
| } else if ( is_singular() ) { | |
| global $post; | |
| setup_postdata( $post ); | |
| if ( '' === ( $excerpt = get_the_excerpt( $post ) ) ) { | |
| $excerpt = wp_kses_post( wp_trim_words( $post->post_content, 55 ) ); | |
| } | |
| ?> | |
| <meta property="og:title" content="<?php esc_attr( the_title() ); ?>" /> | |
| <meta property="og:site_name" content="<?php esc_attr( bloginfo( 'name' ) ); ?>" /> | |
| <meta property="og:url" content="<?php echo esc_attr( get_permalink( $post ) ); ?>" /> | |
| <meta property="og:description" content="<?php echo esc_attr( $excerpt ); ?>" /> | |
| <meta property="og:type" content="article" /> | |
| <?php | |
| if ( has_post_thumbnail() ) { | |
| $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' ); | |
| } else if ( is_null( $custom_image_id ) || empty( $custom_image_id ) ) { | |
| // TODO: svetainės temos aplanke jau turi būti toks failas | |
| $file_path = '/assets/images/default-fb-cover.png'; | |
| if ( file_exists( get_stylesheet_directory() . $file_path ) ) { | |
| $image[0] = get_stylesheet_directory_uri() . $file_path; | |
| } else { | |
| $image[0] = false; | |
| } | |
| } else { | |
| $image = wp_get_attachment_image_src( $custom_image_id, 'full' ); | |
| } | |
| $images = []; | |
| if ( is_array( $image ) && false !== $image[0] ) { | |
| $images[] = trim( $image[0], 'https:, http:' ); // trim, because might be mixed content | |
| } | |
| $media = get_attached_media( 'image' ); | |
| foreach ( $media as $image ) { | |
| $images[] = trim( $image->guid,'https:, http:' ); // trim, because might be mixed content | |
| } | |
| $images_urls = array_unique( $images, SORT_REGULAR ); | |
| $images_urls = array_slice( $images_urls, 0, 5 ); | |
| if ( ! empty( $images_urls ) ) { | |
| $url_protocol = ( is_ssl() ) ? 'https:' : 'http:'; | |
| foreach ( $images_urls as $image_url ) { | |
| ?> | |
| <meta property="og:image" content="<?php echo esc_url( $url_protocol . $image_url ); ?>"/> | |
| <?php | |
| } | |
| } | |
| wp_reset_postdata(); | |
| } | |
| } | |
| add_action( 'wp_head', 'my_site_insert_fb_in_head', 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment