Forum Replies Created

Viewing 15 replies - 1 through 15 (of 161 total)
  • Deviant Media LLC

    (@abretado1985)

    Add the following to your theme functions.php file:

    function restrict_editor_from_homepage() {
        if (!is_admin()) {
            return;
        }
    
        // Ensure the function get_current_screen is defined
        if (!function_exists('get_current_screen')) {
            return;
        }
    
        $screen = get_current_screen();
        if ('page' === $screen->id && isset($_GET['post'])) {
            $post_id = $_GET['post'];
            $home_page_id = get_option('page_on_front');
    
            if ($post_id == $home_page_id) {
                $user = wp_get_current_user();
                if (in_array('editor', $user->roles)) {
                    wp_die('You do not have permission to edit the homepage.');
                }
            }
        }
    }
    
    add_action('current_screen', 'restrict_editor_from_homepage');

    This checks to see if the page is frontpage/homepage and checks users role for editor. If it matches, it will show a message “You do not have permission to edit the homepage.”

    @dscp8g Try this:

    add_action('admin_menu', 'custom_menu');
    
    function custom_menu() {
        add_menu_page(
            'Page Title',              // The text to be displayed in the title tags of the page when the menu is selected
            'Custom Menu',             // The text to be used for the menu
            'manage_options',          // The capability required for this menu to be displayed to the user
            'custom-menu-slug',        // The slug name to refer to this menu by (should be unique for this menu)
            'custom_menu_page',        // The function to be called to output the content for this page
            'dashicons-welcome-write-blog', // Icon URL
            6                           // Position in the menu order
        );
    }
    
    function custom_menu_page() {
        // JavaScript to open the link in a new tab
        echo '<script type="text/javascript">
                window.open("https://www.deviant.media", "_blank");
              </script>';
        // Display a message on the page
        echo '<div class="wrap"><h1>External Link Opened</h1><p>The webpage has been opened in a new window/tab.</p></div>';
    }
    

    To add a custom menu to the admin bar do the following and adjust as necessary.

    add_action('admin_bar_menu', 'custom_menu', 100);
    
    function custom_menu($wp_admin_bar) {
        $args = array(
            'id'    => 'custom_menu',
            'title' => '<span class="ab-icon dashicons-welcome-write-blog"></span> Custom Menu', // title of the menu item with icon
            'href'  => 'https://www.deviant.media', // change URL here
            'meta'  => array(
                'class' => 'custom-menu-class', // you can add more classes here for styling
                'title' => 'Visit Deviant Media', // tooltip
                'target' => '_blank'
            )
        );
        $wp_admin_bar->add_node($args);
    }
    

    Here are a few areas to investigate:

    1. PHP Configuration Limits: Check your server’s PHP configuration, specifically the post_max_size and memory_limit settings. A post with a large amount of content could exceed these limits, causing the request to fail. You might need to increase these values in your php.ini file.
    2. Database Configuration: Large posts could also be hitting limits within your database configuration or the database server’s capacity to handle large inserts. Check your MySQL (or database server) settings, particularly the max_allowed_packet parameter, which might need to be increased.
    3. WordPress Configuration: Although unlikely, it’s worth checking if there are any theme or plugin conflicts that might interfere with inserting large posts. Sometimes, plugins or themes can introduce unexpected behavior or limitations.
    4. Web Server Configuration: Your web server (e.g., Apache, Nginx) might have configuration limits on the size of the request body. For Apache, this is controlled by the LimitRequestBody directive, and for Nginx, the client_max_body_size directive.
    5. Error Logging: Examine WordPress and server error logs for more specific messages regarding the failure. These logs can offer clues that pinpoint the exact issue.
    6. Timeout Issues: If the request takes too long to process, it could hit a timeout limit, resulting in an error. This can be addressed by increasing the timeout settings on your web server or PHP configuration.

    Since the error message indicates a database insertion issue ("code":"db_insert_error","message":"Could not insert post into the database."), I’d particularly focus on database-related settings and logs to start. If you’ve recently updated WordPress or plugins, ensure compatibility and check for known issues related to post insertions.

    Thank for the additional information and hopefully I understood everything correctly. Since you are using Yoast SEO we will leverage its ability to assign a primary category for each post and have any additional categories and their URL’s redirect to it.
    Here is the updated code:

    function redirect_to_yoast_primary_category() {
        if (is_single() && !is_admin() && class_exists('WPSEO_Primary_Term')) {
            global $post;
            
            // Instantiate WPSEO_Primary_Term with 'category' taxonomy
            $wpseo_primary_term = new WPSEO_Primary_Term('category', $post->ID);
            $primary_term_id = $wpseo_primary_term->get_primary_term();
            
            if ($primary_term_id && !is_wp_error($primary_term_id)) {
                $primary_category = get_term($primary_term_id, 'category');
                
                // Verify that get_term did not return an error and returned a valid WP_Term object
                if (!is_wp_error($primary_category) && $primary_category instanceof WP_Term) {
                    $primary_category_slug = $primary_category->slug;
                    $current_cat_slug = get_query_var('category_name');
                    
                    // Proceed with redirection only if the current category slug is valid and not the primary category
                    if (!empty($current_cat_slug) && $current_cat_slug !== $primary_category_slug) {
                        $redirect_url = get_permalink($post->ID);
                        
                        // Ensure the redirect URL is a valid URL to avoid redirection to unexpected locations
                        if (false !== filter_var($redirect_url, FILTER_VALIDATE_URL)) {
                            wp_redirect($redirect_url, 301); // Permanent redirect with HTTP status code 301
                            exit;
                        }
                    }
                }
            }
        }
    }
    add_action('template_redirect', 'redirect_to_yoast_primary_category');
    

    You would use gravity for the dropdown and to build out the questions.

    You would add the code to your functions.php file. Change the “news” slug to match your main category slug.
    ChatGPT is accurate if you ask it correctly and at times it does require some redirection. I use it quite often in my field of work.

    Here’s how you can adjust the function to specifically redirect to the “News” category URL when an article belongs to multiple categories but is accessed through a non-“News” category URL:

    function redirect_to_news_category() {
        if (is_single() && !is_admin()) {
            global $post;
            // Define the main category slug
            $main_category_slug = 'news';
            // Get current category slug from URL
            $current_cat = get_query_var('category_name');
            
            // Check if the current category is not the main category and the post belongs to the main category
            if ($current_cat && $current_cat !== $main_category_slug) {
                // Check if the post belongs to the main category
                if (has_category($main_category_slug, $post->ID)) {
                    // Build the redirect URL
                    $redirect_url = site_url() . '/' . $main_category_slug . '/' . $post->post_name;
                    wp_redirect($redirect_url, 301); // Permanent redirect to the main category URL
                    exit;
                }
            }
        }
    }
    add_action('template_redirect', 'redirect_to_news_category');
    

    What This Code Does:

    • It checks if the current request is for a single post and not within the admin dashboard.
    • It defines “news” as the main category slug and checks the current URL for a different category slug.
    • If the accessed category is not “news” but the post belongs to the “news” category, it redirects to the URL that uses “news” as the category slug.
    • This redirection is done using a 301 HTTP status code, indicating a permanent redirect, which is beneficial for SEO as it passes link equity to the redirected URL.

    Deployment:

    • Add this code to your theme’s functions.php file or a site-specific plugin.
    • Replace 'news' with the actual slug of your main category if it’s different.
    • Test this on a staging site first to ensure it works as expected without impacting other functionalities or SEO negatively.

    Important Considerations:

    • Caching & Performance: If you’re using caching plugins or server-side caching, ensure they’re configured to handle redirects properly to avoid redirect loops or caching the wrong responses.
    • SEO Monitoring: After implementing redirects, keep an eye on Google Search Console for any increase in 404 errors or other crawl issues.
    • Backup: Always back up your website before making changes to the code.

    This custom solution should address your specific requirement to redirect articles accessed through non-main category URLs to the main category URL, consolidating your SEO efforts and reducing duplicate content issues.

    One way I would do this is to use GravityForms and populate the users on the dropdown field. Then the questions, and/or other fields, can be conditional based upon the user selected.

    Your website loads for me on Firefox if I go to http://ladderlead.unaux.com/ but if you try loading it as a secured connection HTTPS, then it will not load. Do you have an SSL installed?

    A workaround involves using the admin_init hook, combined with additional checks when rendering the post editor, to dynamically add page attribute support based on the post’s category. Below is a conceptual approach:

    1. Use the admin_init hook to set up an action for when the post is being edited.
    2. Check if the current post belongs to your specific category.
    3. If it does, add page attribute support to that post.

    Here’s how you can implement it:

    function wpse_custom_init() {
        // Hook into admin_init
        add_action( 'admin_init', 'wpse_conditional_page_attributes_support' );
    }
    
    function wpse_conditional_page_attributes_support() {
        // Check if we're on the post edit screen
        $screen = get_current_screen();
        if ( $screen->id !== 'post' ) return; // Exit if not editing a post
    
        // Check if the global $post is set, and get the post ID
        global $post;
        if ( !isset($post->ID) ) return; // Exit if no post is loaded
    
        // Check if the post belongs to the specific category
        $category_slug = 'your-category-slug'; // Replace with your category slug
        if ( has_category( $category_slug, $post->ID ) ) {
            // Add page attribute support for posts in this category
            add_post_type_support( 'post', 'page-attributes' );
        }
    }
    
    // Hook our function to init
    add_action( 'init', 'wpse_custom_init' );
    

    Replace 'your-category-slug' with the slug of the category you’re targeting. This code snippet checks the current post’s category when you’re on the post edit screen and conditionally adds page attribute support for posts that belong to the specified category.

    Note: Since this approach uses the admin_init and checks the current screen, it’s specifically tailored for the WordPress admin area and is intended to work when editing posts. This method dynamically applies the post type support feature, and it may not persist outside of the context of post editing (e.g., it won’t affect front-end displays or API responses unless those contexts also perform similar checks).

    Your issue with the WordPress block theme not correctly displaying the custom category archive page instead of defaulting to the general archive template could stem from a few reasons. Here are some potential causes and solutions:1. Theme Hierarchy and Naming Convention

    WordPress uses a specific template hierarchy to determine which template file to use for different types of content. For category archives, WordPress looks for template files in the following order:

    • category-slug.php
    • category-id.php
    • category.php
    • archive.php
    • index.php

    Ensure your template naming follows WordPress conventions. Since you are working with a block theme and using the Site Editor to create templates, the issue might be less about naming and more about how WordPress is recognizing your template for specific categories.2. Flush Rewrite Rules

    Sometimes, WordPress doesn’t recognize new templates or changes to the permalink structure until you flush the rewrite rules. This can be done simply by going to Settings > Permalinks and clicking Save Changes without making any changes. This process rewrites the .htaccess file and refreshes WordPress’s understanding of your site structure.3. Check for Conflicts

    Plugin or theme conflicts can cause issues with template loading. Try deactivating all plugins except for those required for your custom post types and taxonomy, and see if the correct template is loaded. If it works, reactivate your plugins one by one to identify the culprit.4. Custom Query in Template

    If your category archive template is being selected but not displaying the correct posts, ensure you’re not overwriting the main query with a custom query without properly resetting it afterward. Use the pre_get_posts action hook to modify the main query if needed, rather than creating new queries in the template.5. Inspect the Template Hierarchy

    Debugging tools like Query Monitor can help you understand which template files WordPress is trying to load and why it might be falling back to the archive.php or another template. This can give you insight into where the process is breaking down.6. Custom Code for Template Selection

    If none of the above solutions work, you might need to use a more manual approach by hooking into the template_include filter to force WordPress to use your custom category archive template under specific conditions. Here’s a basic example:

    add_filter( 'template_include', function( $template ) {
        if ( is_category( 'your-category-slug' ) && !is_singular() ) {
            $new_template = locate_template( array( 'your-custom-template.php' ) );
            if ( '' != $new_template ) {
                return $new_template ;
            }
        }
    
        return $template;
    });
    

    Replace 'your-category-slug' with the slug of your category and 'your-custom-template.php' with the path to your custom template file within your theme.Summary

    Ensure your custom template is correctly named and recognized by WordPress, flush rewrite rules, check for conflicts, and consider using debugging tools to inspect the template hierarchy. If all else fails, manually force template selection with custom code.

    Seems like you are missing some code based on what you outlined. See if adding the following helps:

    // Add function to set the cycle start date
    function set_cycle_start_date($user_id) {
        update_user_meta($user_id, 'cycle_start_date', current_time('Y-m-d'));
    }
    
    // Add function to get the cycle start date
    function get_cycle_start_date($user_id) {
        return get_user_meta($user_id, 'cycle_start_date', true);
    }
    
    // Modify the restart_interest_calculation function
    function restart_interest_calculation($order_id) {
        $order = wc_get_order($order_id);
        if (!$order) return;
    
        $user_id = $order->get_user_id();
        $cycle_start_date = get_cycle_start_date($user_id);
        $cycle_day = (current_time('timestamp') - strtotime($cycle_start_date)) / DAY_IN_SECONDS;
    
        if ($cycle_day <= 15) {
            if ($order->get_status() === 'completed') {
                refund_old_order($order_id);
                start_interest_calculation($order_id);
            }
        } else {
            set_cycle_start_date($user_id); // Reset the cycle
        }
    }
    
    // Modify the handle_15th_day_special_case function
    function handle_15th_day_special_case($order_id) {
        $user_id = get_post_meta($order_id, '_customer_user', true);
        if (!$user_id) return;
    
        $cycle_start_date = get_cycle_start_date($user_id);
        $cycle_day = (current_time('timestamp') - strtotime($cycle_start_date)) / DAY_IN_SECONDS;
    
        if ($cycle_day == 15) {
            return; // Do not calculate interest or refund on the 15th cycle day
        }
    
        start_interest_calculation($order_id);
    }
    

    First, make sure your HTML form has the necessary fields and a submit button. For example:

    <form id="myForm">
      <label for="name">Name:</label>
      <input type="text" id="name" name="name" required>
    
      <label for="email">Email:</label>
      <input type="email" id="email" name="email" required>
    
      <input type="submit" value="Submit">
    </form>
    

    Next, you’ll need to create a JavaScript function that will handle the form submission and perform validation. Place this script just before the closing </body> tag in your HTML document:

    <script>
      document.getElementById("myForm").addEventListener("submit", function(event) {
        // Prevent the form from submitting automatically
        event.preventDefault();
    
        // Get the values from the form
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
    
        // Perform validation
        if (name === "") {
          alert("Please enter your name.");
          return false; // Prevent form submission
        }
    
        if (!isValidEmail(email)) {
          alert("Please enter a valid email address.");
          return false; // Prevent form submission
        }
    
        // If all validation passes, you can submit the form
        // You can add additional processing code here
    
        // For demonstration purposes, let's alert a success message
        alert("Form submitted successfully!");
      });
    
      // Function to validate email format
      function isValidEmail(email) {
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        return emailPattern.test(email);
      }
    </script>
    

    In the above code:

    • We use addEventListener to attach a JavaScript function to the form’s submit event.
    • Inside the event handler function, we prevent the form from submitting automatically using event.preventDefault().
    • We retrieve the values of the name and email fields.
    • We perform validation checks:
    • Check if the name field is empty.
    • Check if the email field matches a valid email pattern using a regular expression (isValidEmail function).
    • If any validation check fails, we display an alert message and return false to prevent form submission.
    • If all validation checks pass, you can add your custom processing code (e.g., sending data to a server) or simply submit the form.

    This code provides basic client-side validation for your form. You can customize it further according to your specific requirements.

    Seeing how big your post is…it could be a WP Memory Limit issue. You can see if adding the following to your wp-config.php file fixes the issue.

    define('WP_MEMORY_LIMIT', '256M'); // or try 512M
    

    Have you tried debugging?

Viewing 15 replies - 1 through 15 (of 161 total)