Skip to content

Instantly share code, notes, and snippets.

@tessak22
Last active February 20, 2020 10:58
Show Gist options
  • Select an option

  • Save tessak22/797fae407d1b2bbb2b8a02de0d24e13f to your computer and use it in GitHub Desktop.

Select an option

Save tessak22/797fae407d1b2bbb2b8a02de0d24e13f to your computer and use it in GitHub Desktop.

Revisions

  1. tessak22 revised this gist Oct 29, 2019. 3 changed files with 66 additions and 38 deletions.
    52 changes: 15 additions & 37 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,43 +1,21 @@
    <?php

    /**
    * Testimonial Block Template.
    *
    * @param array $block The block settings and attributes.
    * @param string $content The block inner HTML (empty).
    * @param bool $is_preview True during AJAX preview.
    * @param (int|string) $post_id The post ID this block is saved to.
    * Testimonial Block
    */
    function register_acf_block_types() {

    // Create id attribute allowing for custom "anchor" value.
    $id = 'testimonial-' . $block['id'];
    if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
    // register a testimonial block.
    acf_register_block_type(array(
    'name' => 'testimonial',
    'title' => __('Testimonial'),
    'description' => __('A custom testimonial block.'),
    'render_template' => 'template-parts/blocks/testimonial/testimonial.php',
    'category' => 'formatting',
    'icon' => 'admin-comments',
    'keywords' => array( 'testimonial', 'quote' ),
    ));
    }

    // Create class attribute allowing for custom "className" and "align" values.
    $className = 'testimonial';
    if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
    }
    if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
    // Check if function exists and hook into setup.
    if( function_exists('acf_register_block_type') ) {
    add_action('acf/init', 'register_acf_block_types');
    }

    ?>
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    <blockquote class="testimonial-blockquote">
    <span class="testimonial-text"><?php echo $text; ?></span>
    <span class="testimonial-author"><?php echo $author; ?></span>
    <span class="testimonial-role"><?php echo $role; ?></span>
    </blockquote>
    <div class="testimonial-image">
    <?php echo wp_get_attachment_image( $image, 'full' ); ?>
    </div>
    <style type="text/css">
    #<?php echo $id; ?> {
    background: <?php echo $background_color; ?>;
    color: <?php echo $text_color; ?>;
    }
    </style>
    </div>
    5 changes: 4 additions & 1 deletion style.css
    Original file line number Diff line number Diff line change
    @@ -1 +1,4 @@
    /* CSS Here */
    .testimonial {display: flex; margin: 0; padding: 0;}
    .testimonial-image {width: 100%; max-width: 500px; height: 500px;}
    .testimonial-author {display: block;}
    .testimonial-role {font-style: italic; font-size: 80%;}
    47 changes: 47 additions & 0 deletions testimonial.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php
    /**
    * Testimonial Block Template.
    *
    * @param array $block The block settings and attributes.
    * @param string $content The block inner HTML (empty).
    * @param bool $is_preview True during AJAX preview.
    * @param (int|string) $post_id The post ID this block is saved to.
    */
    // Create id attribute allowing for custom "anchor" value.
    $id = 'testimonial-' . $block['id'];
    if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
    }
    // Create class attribute allowing for custom "className" and "align" values.
    $className = 'testimonial';
    if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
    }
    if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
    }
    // Load values (field data)
    $text = get_field('testimonial');
    $author = get_field('author');
    $role = get_field('role');
    $image = get_field('image');
    $background_color = get_field('background_color');
    $text_color = get_field('text_color');

    ?>
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    <blockquote class="testimonial-blockquote">
    <span class="testimonial-text"><?php echo $text; ?></span>
    <span class="testimonial-author"><?php echo $author; ?></span>
    <span class="testimonial-role"><?php echo $role; ?></span>
    </blockquote>
    <div class="testimonial-image">
    <?php echo wp_get_attachment_image( $image, 'full' ); ?>
    </div>
    <style type="text/css">
    #<?php echo $id; ?> {
    background: <?php echo $background_color; ?>;
    color: <?php echo $text_color; ?>;
    }
    </style>
    </div>
  2. tessak22 revised this gist Oct 29, 2019. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -24,14 +24,6 @@
    $className .= ' align' . $block['align'];
    }

    // Load values and assign defaults.
    $text = get_field('testimonial') ?: 'Your testimonial here...';
    $author = get_field('author') ?: 'Author name';
    $role = get_field('role') ?: 'Author role';
    $image = get_field('image') ?: 295;
    $background_color = get_field('background_color');
    $text_color = get_field('text_color');

    ?>
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    <blockquote class="testimonial-blockquote">
  3. tessak22 created this gist Oct 29, 2019.
    51 changes: 51 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php

    /**
    * Testimonial Block Template.
    *
    * @param array $block The block settings and attributes.
    * @param string $content The block inner HTML (empty).
    * @param bool $is_preview True during AJAX preview.
    * @param (int|string) $post_id The post ID this block is saved to.
    */

    // Create id attribute allowing for custom "anchor" value.
    $id = 'testimonial-' . $block['id'];
    if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
    }

    // Create class attribute allowing for custom "className" and "align" values.
    $className = 'testimonial';
    if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
    }
    if( !empty($block['align']) ) {
    $className .= ' align' . $block['align'];
    }

    // Load values and assign defaults.
    $text = get_field('testimonial') ?: 'Your testimonial here...';
    $author = get_field('author') ?: 'Author name';
    $role = get_field('role') ?: 'Author role';
    $image = get_field('image') ?: 295;
    $background_color = get_field('background_color');
    $text_color = get_field('text_color');

    ?>
    <div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    <blockquote class="testimonial-blockquote">
    <span class="testimonial-text"><?php echo $text; ?></span>
    <span class="testimonial-author"><?php echo $author; ?></span>
    <span class="testimonial-role"><?php echo $role; ?></span>
    </blockquote>
    <div class="testimonial-image">
    <?php echo wp_get_attachment_image( $image, 'full' ); ?>
    </div>
    <style type="text/css">
    #<?php echo $id; ?> {
    background: <?php echo $background_color; ?>;
    color: <?php echo $text_color; ?>;
    }
    </style>
    </div>
    1 change: 1 addition & 0 deletions style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    /* CSS Here */