$total_pages = $recent_sales->max_num_pages;
if ($total_pages > 1){
echo '<div class="pagination"><ul class="list-unstyled">';
$current_page = max(1, $paged);
$links = paginate_links(array(
'base' => add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '<i class="far fa-angle-left"></i>',
'next_text' => '<i class="far fa-angle-right"></i>',
'add_args' => array(),
'type' => 'array',
'show_all' => true
));
if ( $paged == 1) echo '<li class="pg-prev pg-nav"><a href="javascript:void(0)" class="disabled"><i class="far fa-angle-left"></i></a></li>';
foreach ($links as $key => $link) {
echo "<li>$link</li>";
}
if ( $paged == $total_pages ) echo '<li class="pg-next pg-nav"><a href="javascript:void(0)" class="disabled"><i class="far fa-angle-right"></i></a></li>';
echo '</ul></div>';
}
Category Archives: Wordpress
Order by meta value and query with a meta value | WordPress
$recent_sales = new WP_Query(array(
'paged' => $paged,
'post_type' => 'Property', //custom post type name
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_key' => 'sold_date',
'orderby' => 'meta_value',
'order' => $sortorder == 'oldest' ? 'ASC' : 'DESC',
'meta_query' => array(
array(
'key' => 'property_status',
'value' => 'past-sell'
)
)
));
Custom logo display in WordPress
Following two way we can do it
<?php if ( has_custom_logo() ) : ?>
<a href="/">
<?php
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
?>
<img src="<?php echo $image[0]; ?>" class="custom-logo" alt="Logo" width="146" height="40">
</a>
<?php endif; ?>
<?php if ( has_custom_logo() ) : ?>
<?php the_custom_logo() ?>
<?php endif; ?>
Remove validation of radio and checkbox Contact form 7
Add the below line on your functions.php file
remove_filter('wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10);
Manage wordpress navigation menu dynamically with Nav Walker
Add below codes to your theme functions.php file
function app_nav_init() {
register_nav_menus(array(
'primary' => 'Primary Menu',
'footer-1' => 'Footer Top Menu',
'footer-2' => 'Footer Bottom Menu'
));
}
add_action( 'init', 'app_nav_init' );
//Declare a nav walker class to manage nav custom output
class Footer_2_Walker extends Walker {
/**
* Database fields to use.
* @see Walker::$db_fields
*/
public $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id',
);
function start_el(&$output, $item, $depth=0, $args=array(), $id = 0) {
$object = $item->object;
$type = $item->type;
$title = $item->title;
$description = $item->description;
$permalink = $item->url;
if( $permalink && $permalink != '#' ) {
$output .= '<a href="' . $permalink . '">';
} else {
$output .= '<a href="#">';
}
$output .= $title.'</a>';
}
}
Now, you can call the custom walker from a view to display custom nav menu
<?php
wp_nav_menu( array(
'theme_location' => 'footer-2',
'menu_id' => 'footer-2-menu',
'menu_class' => 'footer-bottom-link',
'items_wrap' => '<div id="%1$s" class="%2$s">%3$s</div>',
'container' => false,
'walker' => new Footer_2_Walker()
) );
?>
Add custom meta to post
Add below codes on you theme functions.php file
function sm_meta_callback( $post ) {
$featured = get_post_meta( $post->ID );
?>
<p>
<div class="sm-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $featured['meta-checkbox'] ) ) checked( $featured['meta-checkbox'][0], 'yes' ); ?> /> Featured this post
</label>
</div>
</p>
<?php
}
function sm_custom_meta() {
add_meta_box( 'sm_meta', __( 'Featured Posts', 'theme-name' ), 'sm_meta_callback', 'post' );
}
add_action( 'add_meta_boxes', 'sm_custom_meta' );
/**
* Saves the custom meta input
*/
function sm_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'sm_nonce' ] ) && wp_verify_nonce( $_POST[ 'sm_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and saves
if( isset( $_POST[ 'meta-checkbox' ] ) ) {
update_post_meta( $post_id, 'meta-checkbox', 'yes' );
} else {
update_post_meta( $post_id, 'meta-checkbox', 'no' );
}
}
add_action( 'save_post', 'sm_meta_save' );
Now, you can fetch posts with custom meta anywhere on your view to display post. Please check below query to fetch posts with meta value
<?php
$args = array(
'posts_per_page' => 5,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes',
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'id',
);
$feature_blog = new WP_Query( $args );
?>
Save contact form 7 data to database with image attachment upload and reCaptcha checking
function create_attachment($source_filepath)
{
$new_image_name = time().'_'.basename($source_filepath);
$filetype = wp_check_filetype($new_image_name, null);
$wp_upload_dir = wp_upload_dir();
$attach_fileName = $wp_upload_dir['path'] . '/' . $new_image_name;
copy($source_filepath, $attach_fileName);
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $attach_fileName,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $new_image_name),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $attach_fileName);
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata($attach_id, $attach_fileName);
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_data;
}
function save_contactform7_before_send_mail( $form_to_DB ) {
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB ) {
$formData = $form_to_DB->get_posted_data();
$leadtype = $formData['lead-type'];
$recaptcha_response = $form_to_DB->recaptcha['response']['success'];
if($recaptcha_response) {
$tableName = '';
$params = [];
global $wpdb;
if($leadtype == 'sell-modal'){
$uploaded_files = $form_to_DB->uploaded_files();
$imgdata = [];
if ($uploaded_files) {
foreach ($uploaded_files as $fieldName => $filepath) {
$imgdata = create_attachment($filepath[0]);
}
}
$your_name = isset($formData['your-name']) ? sanitize_text_field(trim($formData['your-name'])) : "";
$your_email = isset($formData['your-email']) ? sanitize_text_field(trim($formData['your-email'])) : "";
$your_phone = isset($formData['your-phone']) ? sanitize_text_field(trim($formData['your-phone'])) : "";
$your_address = isset($formData['your-address']) ? sanitize_text_field(trim($formData['your-address'])) : "";
$tableName = $wpdb->prefix.'sells';
$params = [
'full_name' => $your_name,
'email' => $your_email,
'phone' => $your_phone,
'address' => $your_address,
'image' => json_encode($imgdata)
];
}
if(!empty($params) && $tableName != ''){
$wpdb->insert($tableName, $params);
}
}
}
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'save_contactform7_before_send_mail' );
Adding contact form 7 to Elementor Custom Widget | WordPress
<?php
/**
* Elementor Widget.
*
* Elementor widget that inserts an embbedable content into the page, from any given URL.
*
* @since 1.0.0
*/
if (!defined('ABSPATH'))
die('No script kiding please');
class Elementor_Contact_Widget extends \Elementor\Widget_Base {
/**
* Get widget name.
*
* Retrieve widget name.
*
* @since 1.0.0
* @access public
*
* @return string Widget name.
*/
public function get_name() {
return 'my-contact';
}
/**
* Get widget title.
*
* Retrieve widget title.
*
* @since 1.0.0
* @access public
*
* @return string Widget title.
*/
public function get_title() {
return __( 'My Contact', 'plugin-name' );
}
/**
* Get widget icon.
*
* Retrieve widget icon.
*
* @since 1.0.0
* @access public
*
* @return string Widget icon.
*/
public function get_icon() {
return 'fa fa-code';
}
/**
* Get widget categories.
*
* Retrieve the list of categories the widget belongs to.
*
* @since 1.0.0
* @access public
*
* @return array Widget categories.
*/
public function get_categories() {
return [ 'basic' ];
}
/**
* Get all entry of contact form 7
* @return array
*/
public function get_contact_form_7_content(){
$posts = get_posts(array(
'post_type' => 'wpcf7_contact_form'
));
$contact7 = [];
$contact7[0] = 'Please select a contact form';
foreach($posts as $k => $post){
$contact7[$post->ID] = __( $post->post_title, 'plugin-name' );
}
return $contact7;
}
/**
* Register widget controls.
*
* Adds different input fields to allow the user to change and customize the widget settings.
*
* @since 1.0.0
* @access protected
*/
protected function _register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => __( 'Content', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'contact_form',
[
'label' => __( 'Contact Form', 'plugin-name' ),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 0,
'options' => $this->get_contact_form_7_content()
]
);
$this->end_controls_section();
}
/**
* Render widget output on the frontend.
*
* Written in PHP and used to generate the final HTML.
*
* @since 1.0.0
* @access protected
*/
protected function render() { ?>
<?php $settings = $this->get_settings_for_display(); ?>
<div class="section-gap contact-request-area">
<div class="container">
<div class="section-heading">
<h3 class="title">We would love to hear from you.</h3>
</div>
<div class="contact-box-wrap">
<?php echo ($settings['contact_form'] > 0) ? do_shortcode('[contact-form-7 id="'. $settings['contact_form'].'"]') : '';?>
</div>
</div>
</div>
<?php }
}
Data download as CSV on WordPress
Below codes will help you to generate a CSV format downloadable file from a source of data list
global $wpdb;
$result = $wpdb->get_results($wpdb->prepare("SELECT * FROM table_name"), ARRAY_A);
$filename = 'filename_';
$date = date("Y-m-d");
ob_end_clean();
$output = fopen('php://output', 'w');
fputcsv( $output, array('Index', 'title', 'name'));
foreach ( $result as $key => $value ) {
fputcsv( $output, array($value['id'], $value['title'], $value['name']) );
}
fclose($output);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); //Forces the browser to download
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . " " . $date . ".csv\";" );
header("Content-Transfer-Encoding: binary");
exit;
Add a repeater inside another repeater of elementor widget development | WordPress
<?php
/**
* Elementor Custom Price Widget for .
*/
namespace Elementor;
if (!defined('ABSPATH'))
die('No script kiding please');
class Elementor_Price_widget extends Widget_Base {
/**
* Get widget name.
* Retrieve Price widget name.
* @access public
* @return string Widget name.
*/
public function get_name()
{
return 'Price';
}
/**
* Get widget title.
* Retrieve Price widget title.
* @access public
* @return string Widget title.
*/
public function get_title()
{
return __( 'Price', 'plugin_name' );
}
/**
* Get widget icon.
* Retrieve Price widget icon.
* @access public
* @return string Widget icon.
*/
public function get_icon()
{
return 'eicon-price-table';
}
/**
* Get widget categories.
* Retrieve the list of categories the Price widget belongs to.
* @access public
* @return array Widget categories.
*/
public function get_categories()
{
return [ 'Text-by-thea' ];
}
/**
* Register Price widget controls.
* Adds different input fields to allow the user to change and customize the widget settings.
* @access protected
*/
protected function _register_controls()
{
$this->start_controls_section(
'content_section',
[
'label' => __( 'Head Content', 'plugin_name' ),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'header_image',
[
'label' => __( 'Image', 'plugin_name' ),
'type' => Controls_Manager::MEDIA,
'default' => [
'url' => GLOBAL_PATH.'/assets/images/section-header-img/Price.png',
]
]
);
$this->add_control(
'top-title',
[
'label' => __( 'Headline Title', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter headline title', 'plugin_name' ),
'default' => 'Text Price Plan'
]
);
$this->add_control(
'field-one',
[
'label' => __( 'First Field Text', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter first field text', 'plugin_name' ),
'default' => 'We bring Swedish relaxation Text, remedial Text, deep tissue'
]
);
$this->add_control(
'field-two',
[
'label' => __( 'Second Field Text', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter second field text', 'plugin_name' ),
'default' => 'Text and corporate chair Text to you.'
]
);
$this->end_controls_section();
$this->start_controls_section(
'content_section_second',
[
'label' => __( 'Content', 'plugin_name' ),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$repeater = new Repeater();
$repeater->add_control(
'bd_color',
[
'label' => __( 'Background Color', 'plugin_name' ),
'type' => Controls_Manager::SELECT,
'options' => [
'bg-orange border-orange' => __( 'Orange', 'plugin_name' ),
'bg-purple border-purple' => __( 'Purple', 'plugin_name' ),
'bg-success-lighter border-success-lighter' => __( 'Green', 'plugin_name' ),
'bg-blue border-blue' => __( 'Blue', 'plugin_name' )
],
]
);
$repeater->add_control(
'fa_icon',
[
'label' => __( 'Image', 'plugin_name' ),
'type' => Controls_Manager::MEDIA,
'default' => [
'url' => Utils::get_placeholder_image_src(),
]
]
);
$repeater->add_control(
'title',
[
'label' => __( 'Title', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter title', 'plugin_name' ),
]
);
$repeater->add_control(
'title_1',
[
'label' => __( 'Second Title', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter second title', 'plugin_name' ),
]
);
$repeater->add_control(
'connecting_slug',
[
'label' => __( 'Connecting Slug', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter Connecting Slug', 'plugin_name' ),
]
);
$repeater->add_control(
'price_text_color',
[
'label' => __( 'Price Text Color', 'plugin_name' ),
'type' => Controls_Manager::SELECT,
'options' => [
'color-orange' => __( 'Orange', 'plugin_name' ),
'color-purple' => __( 'Purple', 'plugin_name' ),
'color-success' => __( 'Green', 'plugin_name' ),
'color-blue' => __( 'Blue', 'plugin_name' )
],
]
);
$this->add_control(
'Price',
[
'label' => __( 'Pricing Two Grid', 'plugin_name' ),
'type' => Controls_Manager::REPEATER,
'fields' => $repeater->get_controls(),
'title_field' => '{{{ title }}} - {{{ connecting_slug }}}',
'default' => [
[
'fa_icon' => Array( 'url' => GLOBAL_PATH.'/assets/images/pricing-plan/sweedish.svg'),
'title' => 'Title One',
'title_1' => 'Text',
'bd_color' => 'bg-orange border-orange',
'price_text_color' => 'color-orange',
'connecting_slug' => 'block1',
],
[
'fa_icon' => Array( 'url' => GLOBAL_PATH.'/assets/images/pricing-plan/mobile-spa-message.svg'),
'title' => 'Title Two',
'title_1' => 'Text',
'bd_color' => 'bg-purple border-purple',
'price_text_color' => 'color-purple',
'connecting_slug' => 'block2'
],
[
'fa_icon' => Array( 'url' => GLOBAL_PATH.'/assets/images/pricing-plan/deep-tissue.svg'),
'title' => 'Title Three',
'title_1' => 'Text',
'bd_color' => 'bg-success-lighter border-success-lighter',
'price_text_color' => 'color-success',
'connecting_slug' => 'block3'
],
[
'fa_icon' => Array( 'url' => GLOBAL_PATH.'/assets/images/pricing-plan/bamboo.svg'),
'title' => 'Title Four',
'title_1' => 'Text',
'bd_color' => 'bg-blue border-blue',
'price_text_color' => 'color-blue',
'connecting_slug' => 'block4'
],
]
]
);
$this->end_controls_section();
$this->start_controls_section(
'content_section_third',
[
'label' => __( 'Inner Price Content', 'plugin_name' ),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$price_repeater = new Repeater();
$price_repeater->add_control(
'price_text',
[
'label' => __( 'Price Text', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter price text', 'plugin_name' ),
]
);
$price_repeater->add_control(
'connecting_slug',
[
'label' => __( 'Connecting Slug', 'plugin_name' ),
'type' => Controls_Manager::TEXT,
'placeholder' => __( 'Enter Connecting Slug', 'plugin_name' ),
]
);
$this->add_control(
'Price_text_repeater',
[
'label' => __( 'Inner Price Text Repeater', 'plugin_name' ),
'type' => Controls_Manager::REPEATER,
'fields' => $price_repeater->get_controls(),
'title_field' => '{{{ price_text }}} - {{{ connecting_slug }}}',
'default' => [
[
'price_text' => '60 minute: $60',
'connecting_slug' => 'block1'
],
[
'price_text' => '60 minute: $60',
'connecting_slug' => 'block2'
],
[
'price_text' => '0 minute: $90',
'connecting_slug' => 'block2'
],
[
'price_text' => '60 minute: $60',
'connecting_slug' => 'block3'
],
[
'price_text' => '0 minute: $90',
'connecting_slug' => 'block3'
],
[
'price_text' => '60 minute: $60',
'connecting_slug' => 'block4'
],
[
'price_text' => '0 minute: $90',
'connecting_slug' => 'block4'
],
]
]
);
$this->end_controls_section();
}
/**
* Render Price widget output on the frontend.
* Written in PHP and used to generate the final HTML.
* @access protected
*/
protected function render()
{
$settings = $this->get_settings_for_display();
?>
<div class="wrapper" id="pricing">
<div class="container">
<div class="section-header text-center pb-20">
<h2><img src="<?=$settings['header_image']['url']?>" alt="service-header-image" class="img-fluid "><?=$settings['top-title']?></h2>
<p class="aw-b2"><?=$settings['field-one']?> <br> <?=$settings['field-two']?></p>
</div>
<div class="row">
<?php if ( $settings['Price'] ): ?>
<?php foreach ( $settings['Price'] as $item ):?>
<div class="col-xl-3 col-lg-6 col-md-6 mb-30">
<div class="single-pricing-plan <?=$item['bd_color']?> radius-6">
<div class="pricing-header">
<img src="<?=$item['fa_icon']['url']?>" alt="pricing-icon" class="img-fluid mb-05">
<h3><?=$item['title']?> <br> <?=$item['title_1']?></h3>
<?php if ( $settings['Price_text_repeater'] ): ?>
<ul class="custom-list custom-service-list">
<?php foreach ( $settings['Price_text_repeater'] as $price ):?>
<?php if ($item['connecting_slug'] === $price['connecting_slug'] ){ ?>
<li class="<?=$item['price_text_color']?>"><?=$price['price_text']?></li>
<?php } ?>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</div>
</div>
<?php endforeach;?>
<?php endif;?>
</div>
</div>
</div>
<?php
}
}