Skip to content

Instantly share code, notes, and snippets.

@rtpHarry
Created September 13, 2022 14:20
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 rtpHarry/3534f17c4089c8493378e97ba8e91dd1 to your computer and use it in GitHub Desktop.
Save rtpHarry/3534f17c4089c8493378e97ba8e91dd1 to your computer and use it in GitHub Desktop.
WordPress Post Grid Layout - Parent Title With Link
<?php
/*
Plugin Name: Post Grid Layout - Parent Title With Link
Plugin URI: https://runthings.dev/
Description: Display the parent page title in the layout editor, with optional link.
Version: 1.0.0
Author: runthings.dev
Author URI: https://runthings.dev/
*/
if (!defined('ABSPATH')) exit; // if direct access
add_action('plugins_loaded', 'rtp_register_parent_title');
function rtp_register_parent_title()
{
add_action('post_grid_layout_elements', 'rtp_add_grid_layout_element');
add_action('post_grid_layout_element_option_rtp_parent_link', 'rtp_post_grid_layout_element_option_rtp_parent_link');
add_action('post_grid_layout_element_rtp_parent_link', 'rtp_post_grid_layout_element_rtp_parent_link');
add_action('post_grid_layout_element_css_rtp_parent_link', 'post_grid_layout_element_css_rtp_parent_link', 10);
}
function rtp_add_grid_layout_element($elements_group)
{
$elements_group['general']['items']['rtp_parent_link'] = array('name' => __('Soka: Parent title with link', 'post-grid'));
return $elements_group;
}
function rtp_post_grid_layout_element_option_rtp_parent_link($parameters)
{
$settings_tabs_field = new settings_tabs_field();
$input_name = isset($parameters['input_name']) ? $parameters['input_name'] : '{input_name}';
$element_data = isset($parameters['element_data']) ? $parameters['element_data'] : array();
$element_index = isset($parameters['index']) ? $parameters['index'] : '';
$custom_class = isset($element_data['custom_class']) ? $element_data['custom_class'] : '';
$prefix_text = isset($element_data['prefix_text']) ? $element_data['prefix_text'] : '';
$color = isset($element_data['color']) ? $element_data['color'] : '';
$font_size = isset($element_data['font_size']) ? $element_data['font_size'] : '';
$font_family = isset($element_data['font_family']) ? $element_data['font_family'] : '';
$margin = isset($element_data['margin']) ? $element_data['margin'] : '';
$text_align = isset($element_data['text_align']) ? $element_data['text_align'] : '';
$link_to = isset($element_data['link_to']) ? $element_data['link_to'] : '';
$link_target = isset($element_data['link_target']) ? $element_data['link_target'] : '';
$char_limit = isset($element_data['char_limit']) ? $element_data['char_limit'] : 0;
$css = isset($element_data['css']) ? $element_data['css'] : '';
$css_hover = isset($element_data['css_hover']) ? $element_data['css_hover'] : '';
?>
<div class="item">
<div class="element-title header">
<span class="remove" onclick="jQuery(this).parent().parent().remove()"><i class="fas fa-times"></i></span>
<span class="sort"><i class="fas fa-sort"></i></span>
<span class="expand"><?php echo __('Soka: Parent post title with link', 'post-grid'); ?></span>
</div>
<div class="element-options options">
<?php
$args = array(
'id' => 'custom_class',
'css_id' => $element_index . '_custom_class',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Wrapper custom class', 'post-grid'),
'details' => __('Set custom class.', 'post-grid'),
'type' => 'text',
'value' => $custom_class,
'default' => '',
'placeholder' => 'css-class',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'prefix_text',
'css_id' => $element_index . '_prefix_text',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Prefix text', 'post-grid'),
'details' => __('Display text before parent post title.', 'post-grid'),
'type' => 'text',
'value' => $prefix_text,
'default' => '',
'placeholder' => '',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'link_to',
'css_id' => $element_index . '_link_to',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Link to', 'post-grid'),
'details' => __('Choose option to link title.', 'post-grid'),
'type' => 'select',
'value' => $link_to,
'default' => 'none',
'args' => apply_filters(
'post_grid_link_to_args',
array(
'post_link' => __('Post link', 'post-grid'),
'none' => __('None', 'post-grid'),
)
),
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'link_target',
'css_id' => $element_index . '_link_target',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Link target', 'post-grid'),
'details' => __('Choose option link target.', 'post-grid'),
'type' => 'select',
'value' => $link_target,
'default' => 'post_link',
'args' => array(
'_blank' => __('_blank', 'post-grid'),
'_parent' => __('_parent', 'post-grid'),
'_self' => __('_self', 'post-grid'),
'_top' => __('_top', 'post-grid'),
),
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'char_limit',
'css_id' => $element_index . '_char_limit',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Character limit', 'post-grid'),
'details' => __('Set character limit.', 'post-grid'),
'type' => 'text',
'value' => $char_limit,
'default' => '',
'placeholder' => '5',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'color',
'css_id' => $element_index . '_color',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Color', 'post-grid'),
'details' => __('Title text color.', 'post-grid'),
'type' => 'colorpicker',
'value' => $color,
'default' => '',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'font_size',
'css_id' => $element_index . '_font_size',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Font size', 'post-grid'),
'details' => __('Set font size.', 'post-grid'),
'type' => 'text',
'value' => $font_size,
'default' => '',
'placeholder' => '14px',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'font_family',
'css_id' => $element_index . '_font_family',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Font family', 'post-grid'),
'details' => __('Set font family.', 'post-grid'),
'type' => 'text',
'value' => $font_family,
'default' => '',
'placeholder' => 'Open Sans',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'margin',
'css_id' => $element_index . '_margin',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Margin', 'post-grid'),
'details' => __('Set margin.', 'post-grid'),
'type' => 'text',
'value' => $margin,
'default' => '',
'placeholder' => '5px 0',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'text_align',
'css_id' => $element_index . '_text_align',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('Text align', 'post-grid'),
'details' => __('Choose text align.', 'post-grid'),
'type' => 'select',
'value' => $text_align,
'default' => 'left',
'args' => array('left' => __('Left', 'post-grid'), 'right' => __('Right', 'post-grid'), 'center' => __('Center', 'post-grid')),
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'css',
'css_id' => $element_index . '_margin',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('CSS', 'post-grid'),
'details' => __('Set css.', 'post-grid'),
'type' => 'textarea',
'value' => $css,
'default' => '',
'placeholder' => '',
);
$settings_tabs_field->generate_field($args);
$args = array(
'id' => 'css_hover',
'css_id' => $element_index . '_margin',
'parent' => $input_name . '[rtp_parent_link]',
'title' => __('CSS hover', 'post-grid'),
'details' => __('Set hover css.', 'post-grid'),
'type' => 'textarea',
'value' => $css_hover,
'default' => '',
'placeholder' => '',
);
$settings_tabs_field->generate_field($args);
ob_start();
?>
<textarea readonly type="text" onclick="this.select();">.element_<?php echo esc_attr($element_index); ?>{}
.element_<?php echo esc_attr($element_index); ?> a{}</textarea>
<?php
$html = ob_get_clean();
$args = array(
'id' => 'use_css',
'title' => __('Use of CSS', 'post-grid'),
'details' => __('Use following class selector to add custom CSS for this element.', 'post-grid'),
'type' => 'custom_html',
'html' => $html,
);
$settings_tabs_field->generate_field($args);
?>
</div>
</div>
<?php
}
function rtp_post_grid_layout_element_rtp_parent_link($args)
{
$element = isset($args['element']) ? $args['element'] : array();
$elementIndex = isset($args['index']) ? $args['index'] : '';
$post_id = isset($args['post_id']) ? $args['post_id'] : '';
if (empty($post_id)) return;
// soka: replace post with parent id
$post_id = wp_get_post_parent_id($post_id);
$layout_id = isset($args['layout_id']) ? $args['layout_id'] : '';
$title = get_the_title($post_id);
$post_link = get_permalink($post_id);
$post_link = apply_filters('post_grid_layout_element_rtp_parent_link_permalink', $post_link, $args);
$link_to = isset($element['link_to']) ? $element['link_to'] : 'post_link';
$link_target = isset($element['link_target']) ? $element['link_target'] : '';
$custom_class = isset($element['custom_class']) ? $element['custom_class'] : '';
$custom_class = do_shortcode($custom_class);
$prefix_text = isset($element['prefix_text']) ? $element['prefix_text'] : '';
$char_limit = isset($element['char_limit']) ? (int) $element['char_limit'] : 0;
$char_end = isset($element['char_end']) ? $element['char_end'] : '...';
if ($char_limit > 0) {
$title = wp_trim_words($title, $char_limit, $char_end);
}
?>
<div class="element element_<?php echo esc_attr($elementIndex); ?> <?php echo esc_attr($custom_class); ?> rtp_parent_link ">
<?php echo esc_html($prefix_text); ?>
<?php if ($link_to == 'post_link') : ?>
<a target="<?php echo esc_attr($link_target); ?>" href="<?php echo esc_url($post_link); ?>"><?php echo esc_html($title); ?></a>
<?php elseif ($link_to == 'custom_link') :
$post_grid_post_settings = get_post_meta($post_id, 'post_grid_post_settings', true);
$thumb_custom_url = !empty($post_grid_post_settings['thumb_custom_url']) ? $post_grid_post_settings['thumb_custom_url'] : $post_link;
?>
<a target="<?php echo esc_attr($link_target); ?>" href="<?php echo esc_url($thumb_custom_url); ?>"><?php echo esc_html($title); ?></a>
<?php else : ?>
<?php echo esc_html($title); ?>
<?php endif; ?>
</div>
<?php
}
function post_grid_layout_element_css_rtp_parent_link($args)
{
$index = isset($args['index']) ? $args['index'] : '';
$element = isset($args['element']) ? $args['element'] : array();
$layout_id = isset($args['layout_id']) ? $args['layout_id'] : '';
$color = isset($element['color']) ? $element['color'] : '';
$font_size = isset($element['font_size']) ? $element['font_size'] : '';
$font_family = isset($element['font_family']) ? $element['font_family'] : '';
$margin = isset($element['margin']) ? $element['margin'] : '';
$text_align = isset($element['text_align']) ? $element['text_align'] : '';
$css = isset($element['css']) ? $element['css'] : '';
$css_hover = isset($element['css_hover']) ? $element['css_hover'] : '';
?>
<style type="text/css">
.layout-<?php echo esc_attr($layout_id); ?>.element_<?php echo esc_attr($index); ?> {
<?php if (!empty($color)) : ?>color: <?php echo esc_attr($color); ?>;
<?php endif; ?><?php if (!empty($font_size)) : ?>font-size: <?php echo esc_attr($font_size); ?>;
<?php endif; ?><?php if (!empty($font_family)) : ?>font-family: <?php echo esc_attr($font_family); ?>;
<?php endif; ?><?php if (!empty($margin)) : ?>margin: <?php echo esc_attr($margin); ?>;
<?php endif; ?><?php if (!empty($text_align)) : ?>text-align: <?php echo esc_attr($text_align); ?>;
<?php endif; ?><?php if (!empty($css)) : ?><?php echo wp_strip_all_tags($css); ?><?php endif; ?>
}
<?php if (!empty($css_hover)) : ?>.layout-<?php echo esc_attr($layout_id); ?>.element_<?php echo esc_attr($index); ?>:hover {
<?php echo wp_strip_all_tags($css_hover); ?>
}
<?php endif; ?>.layout-<?php echo esc_attr($layout_id); ?>.element_<?php echo esc_attr($index); ?>a {
<?php if (!empty($color)) : ?>color: <?php echo esc_attr($color); ?>;
<?php endif; ?><?php if (!empty($font_size)) : ?>font-size: <?php echo esc_attr($font_size); ?>;
<?php endif; ?><?php if (!empty($font_family)) : ?>font-family: <?php echo esc_attr($font_family); ?>;
<?php endif; ?>
}
</style>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment