Changes to directory structure
This commit is contained in:
509
functions.php
509
functions.php
@ -4,86 +4,46 @@
|
||||
|
||||
|
||||
/* Blocks */
|
||||
include get_template_directory(). '/blocks/lcp-viewport/lcp-viewport.php';
|
||||
include get_template_directory(). '/blocks/lcp-sidecontent/lcp-sidecontent.php';
|
||||
include get_template_directory(). '/blocks/lcp-main-area/main-area.php';
|
||||
include get_template_directory(). '/blocks/lcp-header-container/header-container.php';
|
||||
include get_template_directory(). '/blocks/lcp-container/lcp-dynamic-container.php';
|
||||
include get_template_directory(). '/blocks/lcp-key-points/key-points.php';
|
||||
include get_template_directory(). '/blocks/lcp-button/lcp-button.php';
|
||||
include get_template_directory(). '/blocks/lcp-gallery/lcp-gallery.php';
|
||||
|
||||
function my_block_theme_setup() {
|
||||
//include get_template_directory(). '/includes/blocks/lcp-gallery/lcp-gallery.php';
|
||||
|
||||
//include get_template_directory(). '/includes/blocks/lcp-gallery/lcp-gallery.php';
|
||||
include get_template_directory() . '/includes/classes/blocks.php';
|
||||
new Lcp_Blocks();
|
||||
|
||||
|
||||
function lcp_block_theme_setup() {
|
||||
add_theme_support( 'block-templates' );
|
||||
}
|
||||
add_action( 'after_setup_theme', 'my_block_theme_setup' );
|
||||
add_action( 'after_setup_theme', 'lcp_block_theme_setup' );
|
||||
|
||||
function my_theme_enqueue_styles() {
|
||||
function lcp_enqueue() {
|
||||
// Enqueue the theme's main stylesheet (style.css)
|
||||
wp_enqueue_style('my-theme-style', get_stylesheet_uri());
|
||||
wp_enqueue_script('my-theme-script', get_template_directory_uri() . '/script.js');
|
||||
wp_enqueue_style('lcp-style', get_stylesheet_uri());
|
||||
wp_enqueue_script('lcp-script', get_template_directory_uri() . '/script.js');
|
||||
|
||||
// Optionally, enqueue additional stylesheets (if any)
|
||||
// wp_enqueue_style('my-custom-style', get_template_directory_uri() . '/path/to/your/custom-style.css');
|
||||
}
|
||||
|
||||
add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles');
|
||||
add_action('wp_enqueue_scripts', 'lcp_enqueue');
|
||||
|
||||
// Backend enqueue
|
||||
function lcp_enqueue_svg_repeater_script($hook) {
|
||||
// Only enqueue on the theme settings page
|
||||
// if ($hook !== 'settings_page_lcp-theme-settings') {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// CUSTOM POST TYPES
|
||||
|
||||
// Register a Custom Post Type called "Drawer"
|
||||
function register_drawer_cpt() {
|
||||
// Set up the labels for the Drawer CPT
|
||||
$labels = array(
|
||||
'name' => 'Drawers',
|
||||
'singular_name' => 'Drawer',
|
||||
'menu_name' => 'Drawers',
|
||||
'name_admin_bar' => 'Drawer',
|
||||
'add_new' => 'Add New',
|
||||
'add_new_item' => 'Add New Drawer',
|
||||
'new_item' => 'New Drawer',
|
||||
'edit_item' => 'Edit Drawer',
|
||||
'view_item' => 'View Drawer',
|
||||
'all_items' => 'All Drawers',
|
||||
'search_items' => 'Search Drawers',
|
||||
'parent_item_colon' => 'Parent Drawers:',
|
||||
'not_found' => 'No drawers found.',
|
||||
'not_found_in_trash' => 'No drawers found in Trash.',
|
||||
'featured_image' => 'Drawer Image',
|
||||
'set_featured_image' => 'Set drawer image',
|
||||
'remove_featured_image' => 'Remove drawer image',
|
||||
'use_featured_image' => 'Use as drawer image',
|
||||
'archives' => 'Drawer Archives',
|
||||
'insert_into_item' => 'Insert into drawer',
|
||||
'uploaded_to_this_item' => 'Uploaded to this drawer',
|
||||
'filter_items_list' => 'Filter drawers list',
|
||||
// Enqueue the script with a dependency on jQuery
|
||||
wp_enqueue_script(
|
||||
'lcp-svg-repeater', // Handle for the script
|
||||
get_template_directory_uri() . '/assets/js/backend-script.js', // Path to your JS file
|
||||
array(), // Dependency on jQuery
|
||||
null, // No specific version
|
||||
true // Load in footer (true)
|
||||
);
|
||||
|
||||
// Set up the arguments for the Drawer CPT
|
||||
$args = array(
|
||||
'labels' => $labels,
|
||||
'public' => true, // Set this to false if you want it private
|
||||
'publicly_queryable' => true,
|
||||
'show_ui' => true,
|
||||
'show_in_menu' => true,
|
||||
'query_var' => true,
|
||||
'rewrite' => array( 'slug' => 'drawer' ),
|
||||
'capability_type' => 'post',
|
||||
'has_archive' => true,
|
||||
'hierarchical' => false, // Set to true if you want the CPT to behave like pages
|
||||
'menu_position' => 5,
|
||||
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
|
||||
'taxonomies' => array(), // Optionally add taxonomies if needed
|
||||
'show_in_rest' => true, // Enable for Gutenberg editor
|
||||
);
|
||||
|
||||
// Register the Drawer CPT
|
||||
register_post_type( 'drawer', $args );
|
||||
}
|
||||
add_action('admin_enqueue_scripts', 'lcp_enqueue_svg_repeater_script');
|
||||
|
||||
// Hook into the 'init' action to register the CPT
|
||||
add_action( 'init', 'register_drawer_cpt' );
|
||||
|
||||
|
||||
/* KEY POINTS */
|
||||
@ -97,7 +57,51 @@ function add_key_points_meta_box() {
|
||||
'default' // Priority
|
||||
);
|
||||
}
|
||||
add_action('add_meta_boxes', 'add_key_points_meta_box');
|
||||
// Add meta box if setting is turned on
|
||||
$options = get_option('lcp_theme_settings', array());
|
||||
$enable_key_points_meta = isset($options['enable_key_points_meta']) ? $options['enable_key_points_meta'] : 0;
|
||||
|
||||
// Check if 'enable_key_points_meta' is enabled (1), then add the meta box
|
||||
if ($enable_key_points_meta) {
|
||||
// Hook into add_meta_boxes to register the custom meta box
|
||||
add_action('add_meta_boxes', 'add_key_points_meta_box');
|
||||
}
|
||||
|
||||
// Sanitize the SVGs data
|
||||
function lcp_custom_svgs_sanitize($input) {
|
||||
// Ensure the input is an array
|
||||
if (is_array($input)) {
|
||||
foreach ($input as $key => $svg) {
|
||||
// Sanitize the name and path for each SVG
|
||||
if (isset($svg['name'])) {
|
||||
$input[$key]['name'] = sanitize_text_field($svg['name']);
|
||||
}
|
||||
if (isset($svg['path'])) {
|
||||
$input[$key]['path'] = sanitize_textarea_field($svg['path']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
// Existing function to render the theme settings page
|
||||
function render_lcp_theme_settings_page() {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>Theme Settings</h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php
|
||||
// Output necessary settings fields for WordPress to save
|
||||
settings_fields('lcp_theme_settings_group');
|
||||
do_settings_sections('lcp_theme_settings_page'); // This outputs all the fields defined by add_settings_field
|
||||
?>
|
||||
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
// Callback function to render the meta box
|
||||
function render_key_points_meta_box($post) {
|
||||
@ -160,23 +164,148 @@ add_action('save_post', 'save_key_points_meta_box');
|
||||
|
||||
|
||||
|
||||
/* CUSTOM CODE PAGE */
|
||||
function lcp_custom_dashboard_page() {
|
||||
// Register the menu item for the admin page
|
||||
/* BACKEND PAGES */
|
||||
|
||||
// Create backend pages
|
||||
function lcp_backend_pages() {
|
||||
|
||||
// Separator
|
||||
|
||||
// Theme Settings page
|
||||
add_menu_page(
|
||||
'Custom Code Settings', // Page title
|
||||
'Custom Code', // Menu title
|
||||
'manage_options', // Required capability
|
||||
'custom_code_settings', // Menu slug
|
||||
'lcp_custom_code_page', // Callback function
|
||||
'dashicons-admin-code', // Icon
|
||||
100 // Position
|
||||
'Local Content Pro', // Page title
|
||||
'Local Content Pro', // Menu title
|
||||
'manage_options', // Capability required to access this menu
|
||||
'local-content-pro', // Menu slug
|
||||
'render_lcp_theme_settings_page', // Function to render the page
|
||||
'dashicons-admin-generic', // Icon for the menu
|
||||
25 // Position
|
||||
);
|
||||
|
||||
// Register the Custom Code settings page
|
||||
add_menu_page(
|
||||
'Custom Code Inserter', // Page title
|
||||
'Code Inserter', // Menu title
|
||||
'manage_options', // Capability required - admin only
|
||||
'lcp-code-inserter', // Menu slug
|
||||
'custom-code-inserter', // Callback function
|
||||
'', // Icon
|
||||
25
|
||||
|
||||
);
|
||||
}
|
||||
add_action( 'admin_menu', 'lcp_custom_dashboard_page' );
|
||||
add_action( 'admin_menu', 'lcp_backend_pages' );
|
||||
// Function to render the theme settings page
|
||||
// Function to render the theme settings page
|
||||
function lcp_register_custom_svg_settings() {
|
||||
// Register the settings group and option for custom SVGs
|
||||
register_setting(
|
||||
'lcp_theme_settings_group', // Settings group
|
||||
'lcp_custom_svgs', // Option name
|
||||
'lcp_custom_svgs_sanitize' // Sanitization function
|
||||
);
|
||||
|
||||
// The callback function to display the page content
|
||||
function lcp_custom_code_page() {
|
||||
// Add a section to organize the settings
|
||||
add_settings_section(
|
||||
'lcp_custom_svgs_section', // Section ID
|
||||
'Custom SVGs', // Section title
|
||||
'', // Section callback (optional)
|
||||
'lcp_theme_settings_page' // Page where to show this section
|
||||
);
|
||||
|
||||
// Add the custom SVGs field
|
||||
add_settings_field(
|
||||
'lcp_custom_svgs', // Field ID
|
||||
'Add Custom SVGs', // Field title
|
||||
'lcp_custom_svgs_field', // Field callback function
|
||||
'lcp_theme_settings_page', // Page where this field appears
|
||||
'lcp_custom_svgs_section' // Section where this field belongs
|
||||
);
|
||||
}
|
||||
add_action('admin_init', 'lcp_register_custom_svg_settings');
|
||||
|
||||
// Callback to display the custom SVGs field
|
||||
function lcp_custom_svgs_field() {
|
||||
// Retrieve current custom SVGs
|
||||
$custom_svgs = get_option( 'lcp_custom_svgs', array() );
|
||||
|
||||
?>
|
||||
<div id="svg-repeater-container">
|
||||
<?php
|
||||
// Loop through each SVG and display its name and path
|
||||
if (!empty($custom_svgs)) {
|
||||
foreach ($custom_svgs as $svg) {
|
||||
?>
|
||||
<div class="repeater-row">
|
||||
<input type="text" name="lcp_custom_svgs[][name]" value="<?php echo esc_attr($svg['name']); ?>" placeholder="SVG Name" />
|
||||
<textarea name="lcp_custom_svgs[][path]" placeholder="SVG Path"><?php echo esc_textarea($svg['path']); ?></textarea>
|
||||
<button type="button" class="remove-row">Remove</button>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<button type="button" id="add-svg-row">Add SVG</button>
|
||||
<?php
|
||||
}
|
||||
|
||||
// Enqueue the JavaScript for handling the repeater functionality
|
||||
|
||||
|
||||
|
||||
|
||||
// Function to register settings for the theme
|
||||
function lcp_register_settings() {
|
||||
// Register the settings group and the option for lcp_theme_settings
|
||||
register_setting(
|
||||
'lcp_theme_settings_group', // Settings group
|
||||
'lcp_theme_settings', // Option name
|
||||
'lcp_theme_settings_sanitize' // Sanitization callback (optional)
|
||||
);
|
||||
|
||||
// Add a section to organize the settings
|
||||
add_settings_section(
|
||||
'lcp_theme_settings_section', // Section ID
|
||||
'Custom Code Settings', // Section title
|
||||
'', // Section callback (optional)
|
||||
'lcp_theme_settings_page' // Page where to show this section
|
||||
);
|
||||
|
||||
// Add the field for the 'Enable Key Points Meta' checkbox
|
||||
add_settings_field(
|
||||
'enable_key_points_meta', // Field ID
|
||||
'Enable Key Points Meta', // Field title
|
||||
'lcp_enable_key_points_meta_field', // Field callback function
|
||||
'lcp_theme_settings_page', // Page where this field appears
|
||||
'lcp_theme_settings_section' // Section where this field belongs
|
||||
);
|
||||
}
|
||||
add_action('admin_init', 'lcp_register_settings');
|
||||
|
||||
// Function to output the checkbox for 'Enable Key Points Meta'
|
||||
function lcp_enable_key_points_meta_field() {
|
||||
// Retrieve current value for 'enable_key_points_meta'
|
||||
$options = get_option( 'lcp_theme_settings', array() );
|
||||
$checked = isset( $options['enable_key_points_meta'] ) ? $options['enable_key_points_meta'] : false;
|
||||
|
||||
// Output the checkbox
|
||||
echo '<input type="checkbox" name="lcp_theme_settings[enable_key_points_meta]" value="1" ' . checked( $checked, 1, false ) . ' />';
|
||||
}
|
||||
|
||||
// Optional: Sanitization callback function
|
||||
function lcp_theme_settings_sanitize( $input ) {
|
||||
// Ensure that only the expected values are saved (e.g., bool for the checkbox)
|
||||
if ( isset( $input['enable_key_points_meta'] ) ) {
|
||||
$input['enable_key_points_meta'] = (bool) $input['enable_key_points_meta']; // Convert to boolean
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
|
||||
// The callback function to display the custom code inserterpage content
|
||||
function lcp_custom_code_inserter_page() {
|
||||
// Get the stored options from wp_options
|
||||
$custom_code = get_option( 'custom_code_options', array() );
|
||||
|
||||
@ -184,8 +313,19 @@ function lcp_custom_code_page() {
|
||||
<div class="wrap">
|
||||
<h1>Custom Code Settings</h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php settings_fields( 'custom_code_group' ); ?>
|
||||
<?php settings_fields( 'custom_code_group' );
|
||||
if (defined('LCP_DISABLE_CODE_INSERTER') && LCP_DISABLE_CODE_INSERTER === true) {
|
||||
echo "<h2 style=\"margin:10px 5px\"> LCP Custom Code Inserter is currently disabled. </h2>
|
||||
<h3 style=\"margin:5px 5px\"> You can add and modify code snippets, but they will not be in effect. </h3> ";
|
||||
}
|
||||
?>
|
||||
|
||||
<table class="form-table">
|
||||
<tr valign="top">
|
||||
<th scope="row">CSS in Header</th>
|
||||
<td><textarea name="custom_code_options[css_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['css_header'] ) ? $custom_code['css_header'] : '' ); ?></textarea></td>
|
||||
</tr>
|
||||
|
||||
<tr valign="top">
|
||||
<th scope="row">JavaScript in Header</th>
|
||||
<td><textarea name="custom_code_options[js_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['js_header'] ) ? $custom_code['js_header'] : '' ); ?></textarea></td>
|
||||
@ -196,15 +336,7 @@ function lcp_custom_code_page() {
|
||||
<td><textarea name="custom_code_options[js_footer]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['js_footer'] ) ? $custom_code['js_footer'] : '' ); ?></textarea></td>
|
||||
</tr>
|
||||
|
||||
<tr valign="top">
|
||||
<th scope="row">CSS in Header</th>
|
||||
<td><textarea name="custom_code_options[css_header]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['css_header'] ) ? $custom_code['css_header'] : '' ); ?></textarea></td>
|
||||
</tr>
|
||||
|
||||
<tr valign="top">
|
||||
<th scope="row">CSS in Footer</th>
|
||||
<td><textarea name="custom_code_options[css_footer]" rows="10" cols="50"><?php echo esc_textarea( isset( $custom_code['css_footer'] ) ? $custom_code['css_footer'] : '' ); ?></textarea></td>
|
||||
</tr>
|
||||
</table>
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
@ -212,6 +344,7 @@ function lcp_custom_code_page() {
|
||||
<?php
|
||||
}
|
||||
|
||||
// Register a custom code setting in wp_options to store the custom code snippets
|
||||
function lcp_custom_code_settings_init() {
|
||||
register_setting( 'custom_code_group', 'custom_code_options', 'sanitize_custom_code' );
|
||||
}
|
||||
@ -236,39 +369,40 @@ function sanitize_custom_code( $input ) {
|
||||
return $input;
|
||||
}
|
||||
function lcp_custom_code_output($hook) {
|
||||
// Retrieve the custom code options
|
||||
$custom_code = get_option( 'custom_code_options', array() );
|
||||
// Disable the custom code inserter if wp_env is set
|
||||
if (defined('LCP_DISABLE_CODE_INSERTER') && LCP_DISABLE_CODE_INSERTER === true) {
|
||||
// Retrieve the custom code options
|
||||
$custom_code = get_option( 'custom_code_options', array() );
|
||||
|
||||
// Prevent execution in the admin area
|
||||
if ( is_admin() ) return;
|
||||
// Prevent execution in the admin area
|
||||
if ( is_admin() ) return;
|
||||
|
||||
// Check if the hook is for the header
|
||||
if ($hook == "header") {
|
||||
// Check and output JS for header
|
||||
if ( isset( $custom_code['js_header'] ) && ! empty( $custom_code['js_header'] ) ) {
|
||||
echo '<script type="text/javascript">' . $custom_code['js_header'] . '</script>';
|
||||
// Check if the hook is for the header
|
||||
if ($hook == "header") {
|
||||
// Check and output JS for header
|
||||
if ( isset( $custom_code['js_header'] ) && ! empty( $custom_code['js_header'] ) ) {
|
||||
echo '<script type="text/javascript">' . $custom_code['js_header'] . '</script>';
|
||||
}
|
||||
|
||||
// Check and output CSS for header
|
||||
if ( isset( $custom_code['css_header'] ) && ! empty( $custom_code['css_header'] ) ) {
|
||||
echo '<style type="text/css">' . $custom_code['css_header'] . '</style>';
|
||||
}
|
||||
}
|
||||
|
||||
// Check and output CSS for header
|
||||
if ( isset( $custom_code['css_header'] ) && ! empty( $custom_code['css_header'] ) ) {
|
||||
echo '<style type="text/css">' . $custom_code['css_header'] . '</style>';
|
||||
}
|
||||
}
|
||||
// Check if the hook is for the footer
|
||||
if ($hook == "footer") {
|
||||
// Check and output JS for footer
|
||||
if ( isset( $custom_code['js_footer'] ) && ! empty( $custom_code['js_footer'] ) ) {
|
||||
echo '<script type="text/javascript">' . $custom_code['js_footer'] . '</script>';
|
||||
}
|
||||
|
||||
// Check if the hook is for the footer
|
||||
if ($hook == "footer") {
|
||||
// Check and output JS for footer
|
||||
if ( isset( $custom_code['js_footer'] ) && ! empty( $custom_code['js_footer'] ) ) {
|
||||
echo '<script type="text/javascript">' . $custom_code['js_footer'] . '</script>';
|
||||
}
|
||||
|
||||
// Check and output CSS for footer
|
||||
if ( isset( $custom_code['css_footer'] ) && ! empty( $custom_code['css_footer'] ) ) {
|
||||
echo '<style type="text/css">' . $custom_code['css_footer'] . '</style>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Attach to wp_head and wp_footer, passing the location as a parameter
|
||||
add_action( 'wp_head', function() {
|
||||
lcp_custom_code_output('header');
|
||||
@ -280,155 +414,6 @@ add_action( 'wp_footer', function() {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* ------------------------ POPUPS ------------------------------- */
|
||||
|
||||
|
||||
/**
|
||||
* Register the Custom Post Type (CPT) 'lcp-popups'
|
||||
*/
|
||||
function lcp_popups_custom_post_type() {
|
||||
$labels = array(
|
||||
'name' => 'LCP Popups',
|
||||
'singular_name' => 'LCP Popup',
|
||||
'menu_name' => 'LCP Popups',
|
||||
'name_admin_bar' => 'LCP Popup',
|
||||
'add_new' => 'Add New',
|
||||
'add_new_item' => 'Add New Popup',
|
||||
'new_item' => 'New Popup',
|
||||
'edit_item' => 'Edit Popup',
|
||||
'view_item' => 'View Popup',
|
||||
'all_items' => 'All Popups',
|
||||
'search_items' => 'Search Popups',
|
||||
'not_found' => 'No popups found.',
|
||||
'not_found_in_trash' => 'No popups found in Trash.',
|
||||
'parent_item_colon' => '',
|
||||
'menu_icon' => 'dashicons-lightbulb', // Optional: Set an icon
|
||||
);
|
||||
|
||||
$args = array(
|
||||
'labels' => $labels,
|
||||
'public' => true,
|
||||
'show_ui' => true,
|
||||
'show_in_menu' => true,
|
||||
'show_in_admin_bar' => true,
|
||||
'menu_position' => 20,
|
||||
'supports' => array( 'title', 'editor' ), // You can add other supports like 'thumbnail', 'excerpt', etc.
|
||||
'has_archive' => true,
|
||||
'rewrite' => array( 'slug' => 'lcp-popups' ),
|
||||
'show_in_rest' => true,
|
||||
);
|
||||
|
||||
register_post_type( 'lcp-popup', $args );
|
||||
}
|
||||
add_action( 'init', 'lcp_popups_custom_post_type' );
|
||||
|
||||
/**
|
||||
* Add a meta box with a toggle switch for "Show on All Pages"
|
||||
*/
|
||||
function lcp_popups_add_meta_box() {
|
||||
add_meta_box(
|
||||
'lcp_popups_toggle_meta_box', // Meta box ID
|
||||
'Popup Settings', // Meta box title
|
||||
'lcp_popups_meta_box_callback', // Callback function
|
||||
'lcp_popups', // Post type
|
||||
'side', // Context (normal, side, etc.)
|
||||
'high' // Priority (default, low, high)
|
||||
);
|
||||
}
|
||||
add_action( 'add_meta_boxes', 'lcp_popups_add_meta_box' );
|
||||
|
||||
/**
|
||||
* Callback function to display the meta box content
|
||||
*/
|
||||
function lcp_popups_meta_box_callback( $post ) {
|
||||
// Add a nonce for security
|
||||
wp_nonce_field( 'lcp_popups_nonce_action', 'lcp_popups_nonce' );
|
||||
|
||||
// Get the current value of the toggle (default to false)
|
||||
$show_on_all_pages = get_post_meta( $post->ID, '_lcp_popups_show_on_all_pages', true );
|
||||
|
||||
// Display the toggle switch (checkbox)
|
||||
?>
|
||||
<p>
|
||||
<label for="lcp_popups_show_on_all_pages">
|
||||
<input type="checkbox" id="lcp_popups_show_on_all_pages" name="lcp_popups_show_on_all_pages" value="1" <?php checked( $show_on_all_pages, 1 ); ?> />
|
||||
Show on All Pages
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the meta box data when the post is saved
|
||||
*/
|
||||
function lcp_popups_save_meta_box_data( $post_id ) {
|
||||
// Check if our nonce is set
|
||||
if ( ! isset( $_POST['lcp_popups_nonce'] ) ) {
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
$nonce = $_POST['lcp_popups_nonce'];
|
||||
|
||||
// Verify that the nonce is valid
|
||||
if ( ! wp_verify_nonce( $nonce, 'lcp_popups_nonce_action' ) ) {
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
// Check if this is an autosave
|
||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
// Check if the current user has permission to edit the post
|
||||
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
// Sanitize and save the value of the toggle
|
||||
$show_on_all_pages = isset( $_POST['lcp_popups_show_on_all_pages'] ) ? 1 : 0;
|
||||
update_post_meta( $post_id, '_lcp_popups_show_on_all_pages', $show_on_all_pages );
|
||||
|
||||
// If the toggle is checked, update the wp_options field
|
||||
if ( $show_on_all_pages ) {
|
||||
$popups = get_option( 'lcp_popups_every_page', [] );
|
||||
$popups[] = $post_id; // Add the current post ID to the array
|
||||
update_option( 'lcp_popups_every_page', array_unique( $popups ) ); // Save the updated array (remove duplicates)
|
||||
} else {
|
||||
// If unchecked, remove the post ID from the array
|
||||
$popups = get_option( 'lcp_popups_every_page', [] );
|
||||
$popups = array_filter( $popups, function( $id ) use ( $post_id ) {
|
||||
return $id !== $post_id;
|
||||
});
|
||||
update_option( 'lcp_popups_every_page', array_values( $popups ) ); // Reindex the array
|
||||
}
|
||||
}
|
||||
add_action( 'save_post', 'lcp_popups_save_meta_box_data' );
|
||||
|
||||
function lcp_display_popups_on_every_page() {
|
||||
echo '<div class="lcp-popup">' . $content . '</div>';
|
||||
// Get the array of post IDs from the wp_option
|
||||
$post_ids = get_option('lcp_popups_every_page');
|
||||
|
||||
if (!empty($post_ids) && is_array($post_ids)) {
|
||||
// Loop through the post IDs and get the content for each post
|
||||
foreach ($post_ids as $post_id) {
|
||||
// Make sure the post exists and get its content
|
||||
$post = get_post($post_id);
|
||||
if ($post) {
|
||||
// Get the content and wrap it with a class
|
||||
$content = apply_filters('the_content', $post->post_content);
|
||||
|
||||
// Output the popup content in the footer
|
||||
echo '<div class="lcp-popup">' . $content . '</div>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
add_action('wp_footer', 'lcp_display_popups_on_every_page');
|
||||
|
||||
|
||||
// Hook into wp_footer to add the login form in the footer
|
||||
function add_login_form_to_footer() {
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user