-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsalcodes.php
More file actions
81 lines (66 loc) · 2.57 KB
/
salcodes.php
File metadata and controls
81 lines (66 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
Plugin Name: Salcodes
Version: 1.0
Description: Three examples to showcase how Shortcodes work in WordPress.
Author: Salman Ravoof
Author URI: https://www.salmanravoof.com/
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: salcodes
*/
/** Registering the Shortcodes only after WordPress has finished loading completely ('init' hook) */
function salcodes_init(){
add_shortcode( 'current_year', 'salcodes_year' );
add_shortcode( 'cta_button', 'salcodes_cta' );
add_shortcode( 'boxed', 'salcodes_boxed' );
}
add_action('init', 'salcodes_init');
/**
* [current_year] returns the Current Year as a 4-digit string.
* @return string Current Year
*/
function salcodes_year() {
return getdate()['year'];
}
/**
* [cta_button] returns HTML code for a CTA Button.
* @return string Button HTML Code
*/
function salcodes_cta( $atts ) {
$url = home_url();
$a = shortcode_atts( array(
'link' => $url,
'id' => 'salcodes',
'color' => 'blue',
'size' => '',
'label' => 'Button',
'target' => '_self'
), $atts );
$output = '<p><a href="' . esc_url( $a['link'] ) . '" id="' . esc_attr( $a['id'] ) . '" class="button ' . esc_attr( $a['color'] ) . ' ' . esc_attr( $a['size'] ) . '" target="' . esc_attr($a['target']) . '">' . esc_attr( $a['label'] ) . '</a></p>';
return $output;
}
/**
* [boxed] returns HTML code for a content box with colored titles.
* @return string HTML code for boxed content.
*/
function salcodes_boxed( $atts, $content = null, $tag = '' ) {
$a = shortcode_atts( array(
'title' => 'Title',
'title_color' => 'white',
'color' => 'blue',
), $atts );
$output = '<div class="salcodes-boxed" style="border:2px solid ' . esc_attr( $a['color'] ) . ';">'.'<div class="salcodes-boxed-title" style="background-color:' . esc_attr( $a['color'] ) . ';"><h3 style="color:' . esc_attr( $a['title_color'] ) . ';">' . esc_attr( $a['title'] ) . '</h3></div>'.'<div class="salcodes-boxed-content"><p>' . esc_attr( $content ) . '</p></div>'.'</div>';
return $output;
}
/** Enqueuing the Stylesheet for Salcodes */
function salcodes_enqueue_scripts() {
global $post;
$has_shortcode = has_shortcode( $post->post_content, 'cta_button' ) || has_shortcode( $post->post_content, 'boxed' );
if( is_a( $post, 'WP_Post' ) && $has_shortcode ) {
wp_register_style( 'salcodes-stylesheet', plugin_dir_url( __FILE__ ) . 'css/style.css' );
wp_enqueue_style( 'salcodes-stylesheet' );
}
}
add_action( 'wp_enqueue_scripts', 'salcodes_enqueue_scripts');
?>