Deviant Media LLC
Forum Replies Created
-
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’ssubmit
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.
Forum: Fixing WordPress
In reply to: Can’t get access to edit one particular postSeeing 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?
Forum: Fixing WordPress
In reply to: category as part of the slug/url only for specific categoriesTo achieve your goal of including the category in the URL slug only for specific categories in WordPress, you will need to write a custom function that hooks into WordPress’s URL rewriting system. Here’s a step-by-step guide to accomplish this:
- Identify Categories for Special Treatment: First, you need to identify the categories (e.g., ‘newsletter’, ‘podcast’, ‘youtube’) for which you want the category to be part of the URL.
- Create a Custom Function for URL Rewriting: You need to write a function that modifies the permalink structure based on the category of the post.
- Hook the Function into WordPress: Use WordPress hooks to integrate your custom function into the WordPress URL rewriting system.
function custom_permalink_structure($post_link, $post, $leavename) { if (is_object($post) && $post->post_type == 'post') { $categories = get_the_category($post->ID); if (!empty($categories)) { // Define categories that require a custom URL structure $special_categories = array('newsletter', 'podcast', 'youtube'); foreach ($categories as $category) { if (in_array($category->slug, $special_categories)) { return home_url($category->slug . '/' . $post->post_name . '/'); } } } } return $post_link; } add_filter('post_link', 'custom_permalink_structure', 10, 3);
Here’s what this code does:
- It checks the categories of each post.
- If the post belongs to one of the specified categories (‘newsletter’, ‘podcast’, ‘youtube’), it modifies the permalink to include the category slug.
- For posts in other categories, it leaves the permalink structure as-is.
- Flush Rewrite Rules: After adding this code to your theme’s
functions.php
file, you need to flush the rewrite rules to apply these changes. You can do this by going to Settings → Permalinks in your WordPress admin and just clicking ‘Save Changes’. - Handling Posts in Multiple Categories: If a post belongs to multiple categories, including both special and regular ones, you need to decide which category slug should be used in the URL. The above code will use whichever special category it finds first. You might want to add additional logic to handle such cases as per your requirements.
Forum: Developing with WordPress
In reply to: WP_Query sort by multiple custom fieldsMaybe I am not understanding your request. You want to show events in ASC followed by News DESC, as two separate lists? If so, try this:
add_action('pre_get_posts', function ($query) { if (!is_admin() && $query->is_archive('news') && $query->is_main_query()) { // Disable the main query $query->set('posts_per_page', 0); // Query for events $events_query = new WP_Query([ 'post_type' => 'news', 'meta_key' => 'type', 'meta_value' => 'event', 'orderby' => 'date', 'order' => 'ASC', // Add other necessary arguments ]); // Query for news $news_query = new WP_Query([ 'post_type' => 'news', 'meta_key' => 'type', 'meta_value' => 'news', 'orderby' => 'date', 'order' => 'DESC', // Add other necessary arguments ]); // Combine results $combined_posts = array_merge($events_query->posts, $news_query->posts); // Override the main query $query->posts = $combined_posts; $query->post_count = count($combined_posts); } });
Forum: Fixing WordPress
In reply to: How to Create different image folder for different forms?I do not know of a plugin that currently does this. A custom plugin would be best I think.
Forum: Developing with WordPress
In reply to: WP_Query sort by multiple custom fieldsTry this:
add_action('pre_get_posts', function ($query) { if (!is_admin() and $query->is_archive('news') and $query->is_main_query()) { $meta_query = []; $meta_query[] = [ 'relation' => 'OR', [ 'event_clause' => [ 'key' => 'type', 'value' => 'event', 'compare' => '=', ], 'date_clause' => [ 'key' => 'date', 'value' => date('Ymd'), 'compare' => '>', 'type' => 'DATE' ], ], [ 'news_clause' => [ 'key' => 'type', 'value' => 'news', 'compare' => '=' ], ] ]; $orderby = [ 'event_clause' => 'ASC', 'date_clause' => 'ASC', 'news_clause' => 'DESC' ]; $query->set('meta_query', $meta_query); $query->set('orderby', $orderby); } });
Updated the structure of the meta query to include ‘date_clause’ for events. This will ensure that events are sorted by both ‘type’ and ‘date’. Added ‘date_clause’ to the ‘orderby’ array to sort events by date as well.
This should display upcoming events first in ascending order and then display news in descending order.Forum: Developing with WordPress
In reply to: Create a survey and generate a pdf report of the resultsYou can use WooCommerce for payment and GravityForms with Survey Addon + Gravity PDF.
Here is a run down how I see this play out.
1 – Customer purchases Access Code and is able to view a random code in the order confirmation and when viewing order details.
2 – The random access code is entered into a database table (orderID, Access Code, is_used)
3 – Customer visits survey page which only displays an input field for the access code which is validated against the database. If no code exist = Not valid, if code exists but used (is_used) = access code has been used, if code exists and not used = display gravity form “Survey”.
4 – Upon successfully completion and submission of the survey, the access code is marked “used”.
5 – Gravity Forms converts the results to PDF and emails it to the customer.
You can create a custom plugin that that ties all this together. Let me know if you have any questions.Forum: Developing with WordPress
In reply to: Stopping Form Autofill without PluginThe issue you’re facing with browser autofill in WooCommerce can indeed be a bit tricky, especially if standard attributes like
autocomplete="off"
aren’t working as expected. This is partly because modern browsers often ignore theautocomplete="off"
attribute for password fields to enhance user experience. However, we can try a different approach using a combination of PHP and JavaScript to tackle this.Here’s a custom solution you can implement:
- PHP Code to Add a Custom JavaScript: We’ll enqueue a custom JavaScript file in your WordPress theme that specifically targets the WooCommerce account details template. Add this code to your theme’s
functions.php
file:php
function cw_disable_autofill_on_woocommerce_pages() { if ( is_account_page() ) { wp_enqueue_script('cw-disable-autofill', get_stylesheet_directory_uri() . '/js/disable-autofill.js', array('jquery'), '', true); } } add_action('wp_enqueue_scripts', 'cw_disable_autofill_on_woocommerce_pages');
This code checks if the current page is an account page in WooCommerce. If it is, it enqueues a JavaScript file named
disable-autofill.js
.JavaScript to Manipulate the Password Fields: Create a file named
disable-autofill.js
in thejs
directory of your theme. Add the following JavaScript code:javascript
jQuery(document).ready(function($) { // Target password fields $('input[type="password"]').each(function() { // Change the field type to text and back to password to trick the browser $(this).attr('type', 'text'); setTimeout(() => { $(this).attr('type', 'password'); }, 50); }); });
This script will momentarily change the type of password fields frompassword
totext
and then back topassword
. This is a workaround to prevent browsers from recognizing these fields as typical password fields and thus, not autofilling them.- Testing and Adjusting: After implementing these changes, clear your browser cache and test the account details page. You should see that the browser no longer autofills the password fields.
Remember, while this approach should work in most cases, browser behavior can vary, and some updates or specific settings might override this behavior. This solution is more of a workaround than a guaranteed fix due to the nature of how browsers handle autofill functionalities.
Forum: Developing with WordPress
In reply to: How to validate url query var?Hook Into
pre_get_posts
Action: Thepre_get_posts
action hook allows you to alter the query before it is executed. This is where you can add your validation logic.function validate_custom_query_var( $query ) { if ( !is_admin() && $query->is_main_query() ) { // Assuming 'my_custom_var' is your custom query var $value = get_query_var( 'my_custom_var', false ); // Validate the value of the custom query var if ( $value !== false ) { if ( !is_valid_custom_var( $value ) ) { // If invalid, force a 404 error $query->set_404(); status_header( 404 ); nocache_headers(); } } } } add_action( 'pre_get_posts', 'validate_custom_query_var' );
Define Your Validation Function: Implement the
is_valid_custom_var
function to validate the custom query variable’s value according to your requirements.function is_valid_custom_var( $value ) { // Define your validation logic here // Return true if valid, false otherwise return in_array($value, ['allowed_value1', 'allowed_value2']); // Example }
Remember to replace
'my_custom_var'
with your actual custom query variable name and modify the validation logic inis_valid_custom_var
to suit your specific requirements.Forum: Fixing WordPress
In reply to: Help me: “Parse error: syntax error, unexpected ‘}'”What plugin did you install? Can you share line 247 from your functions.php file?
Forum: Fixing WordPress
In reply to: I deleted all data off my website and it’s slower than ever beforeYour database is probably all fragmented. Have you optimized your database since you deleted a lot of data?
Forum: Requests and Feedback
In reply to: Slider HyperlinkSee if adding this code to your functions.php file helps:
function add_slider_clickable_link() { echo " <script> jQuery(document).ready(function($) { $('.carousel-item').each(function() { var link = $(this).find('a').attr('href'); $(this).css('cursor', 'pointer'); $(this).on('click', function() { window.location.href = link; }); }); }); </script> "; } add_action('wp_footer', 'add_slider_clickable_link');
Here’s a step-by-step explanation of what the code does:
- Wait for the document to be ready before executing the code.
- Select all elements with the class
carousel-item
. - For each slide, do the following: a. Find the anchor element (
<a>
) within the slide and get the value of itshref
attribute (which is the link). b. Change the cursor style to a pointer when hovering over the slide to indicate that it is clickable. c. Add an event listener for the click event. When the slide is clicked, it redirects the user to the link.
Forum: Developing with WordPress
In reply to: media upload APIHere are a few potential causes and solutions:
- File Permissions: Check the file permissions on the server where your WordPress site is hosted. Make sure that the
wp-content/uploads
directory has the appropriate permissions to allow file uploads. Typically, the directory should have755
or775
permissions. - Server Configuration: Check the server configuration to ensure that the server allows file uploads. For example, if you are using Apache, make sure that the
AllowOverride
directive is set toAll
in the Apache configuration file. This allows the.htaccess
file in the WordPress directory to enable file uploads. - PHP Configuration: Check the PHP configuration (
php.ini
file) on your server. Make sure that thefile_uploads
directive is set toOn
, and that theupload_max_filesize
andpost_max_size
directives are set to a value that is larger than the size of the image you are trying to upload. - WordPress Configuration: Check the WordPress configuration (
wp-config.php
file) on your server. Make sure that theWP_MEMORY_LIMIT
andWP_MAX_MEMORY_LIMIT
directives are set to a value that is sufficient for image uploads. - Image Size: Check the size of the image you are trying to upload. If the image is too large, it may exceed the maximum file size allowed by your server or WordPress configuration. Try reducing the image size or compressing the image and try uploading again.
- Retool Configuration: Check the configuration of your Retool application. Make sure that the API key or user account being used has the necessary permissions to upload files, and that the API endpoint and HTTP headers are set up correctly.
- Error Logs: Check the error logs on your server to see if there are any error messages related to the image upload. The error logs may provide more information about the cause of the error.
Forum: Developing with WordPress
In reply to: plugin to create dynamic generated pdf printablesHere’s an example of how you could create a simple PDF with layers using PHP and the
FPDF
library:- Frontend:
- Create a web page with radio buttons or drop-down selections for the user to choose the layers they want to include in the PDF.
- Use JavaScript to collect the user’s selections and send them to the server as a JSON object (using AJAX or by submitting a form).
- Backend (PHP):
- Receive the user’s selections.
- Use the
FPDF
library to create a new PDF document. - Add the selected layers to the PDF document based on the user’s selections. Each layer could be a separate image or PDF file that you overlay on the main PDF document.
- Output the PDF to the user’s browser as a downloadable file or display it in an embedded PDF viewer on the web page.
Here is an example of a PHP script using the
FPDF
library to create a simple PDF with layers:require('fpdf.php'); // Define the layers $layer1 = "Layer 1 content"; $layer2 = "Layer 2 content"; $layer3 = "Layer 3 content"; // Get user's selections (for example, from a JSON object) $user_selections = array( "layer1" => true, "layer2" => false, "layer3" => true ); // Create a new PDF $pdf = new FPDF(); $pdf->AddPage(); // Add the selected layers to the PDF if ($user_selections["layer1"]) { $pdf->SetFont('Arial', 'B', 16); $pdf->Cell(40, 10, $layer1); $pdf->Ln(); } if ($user_selections["layer2"]) { $pdf->SetFont('Arial', 'B', 16); $pdf->Cell(40, 10, $layer2); $pdf->Ln(); } if ($user_selections["layer3"]) { $pdf->SetFont('Arial', 'B', 16); $pdf->Cell(40, 10, $layer3); $pdf->Ln(); } // Output the PDF to the browser $pdf->Output();
In this example, we define three layers as strings and get the user’s selections in an associative array. We then create a new PDF using the
FPDF
library and add the selected layers to the PDF based on the user’s selections. Finally, we output the PDF to the browser. You can then send this file to the user’s browser for download or display it in an embedded PDF viewer on the web page.Note that this is just a simple example to give you an idea of how you could implement this functionality using PHP. In a real-world scenario, you would need to handle more complex layers (e.g., images or other PDF files), error handling, and user input validation.
- We use