I'm developing a plugin to wordpress using WordPress Plugin Boilerplate.
Therefore, I need to create a shortcode to show up my data in one page for the user.
Is it correct to concatenate the HTML as below?
function custom_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'type' => 'type'
),
$atts
);
//get all entries (page 1 with 999999 entries per page should do it:)
$data = Data::get_entries( $atts['type']);
ob_start();
?>
<div class="row">
<header><h3>Expenses</h3></header>
<p>
<span class="cf-entries-header app-name">Expense Origin</span>
<span class="cf-entries-header page-name">Expense Value</span>
</p>
<?php
foreach ( (array)$data['entries'] as $entry ) {
echo '<p class="cf-entry">’ .
‘<span class="exp-origin">' . $entry['data']['origin'] . '</span>’ .
‘<span class="exp-value">' . $entry['data']['value'] . '</span>’;
};
?>
</div>
<?php
$output = ob_get_clean();
return $output;
};
}
add_shortcode( 'cf_app_entries', 'cf_app_entries' );
No this is not right way..
You can't echo all content in shortcode..
You need to bind all content (HTML) in one variable and return this variable
Example
function loginform() {
$html = '<form class="custom_login_form" rel="" method="POST" role="form" action="" autocomplete="off">
<div class="form-group">
<div class="input-container">
<input id="useremail" type="text" class="form-control input-field" name="email" placeholder="Email">
</div>
</div>
<div class="twice-box">
<div class="form-group">
<div class="input-container">
<input id="userpassword" type="password" class="form-control input-field" name="userpassword" placeholder="Password">
</div>
</div>
<div class="form-group text-center">
Login
</div>
</div>
</form>';
return $html;
}
add_shortcode('login-form', 'loginform');
Please check this ShortCode..
If any issue please let me know..
I will be happy to help you..
Related
i know it's a duplicate question, but please be kind with me, i have to solve this. I have a custom form in Wordpress, sending an email template via AJAX. Everything is working fine, except those single fields attachments in the image below. those input fields are triggered by jquery to receive the image and i have tested if the form input file are not empty with Jquery before sending the email. When i put the hard code it's works, otherwise it's not. Please guide me to solve this. Any help would be appreciate.
my HTML form:
<form id="tratamento_form" method="POST" action="#" enctype="multipart/form-data">
<div class="form-group ">
<label for="name" class="">Nome completo</label>
<input id="name" class="form-control " placeholder="José Aparecido " type="text" name="nome" id="name" autocomplete="off">
</div>
<div class="form-group ">
<label for="message" class="" >Selecione 6 fotos</label>
<div class="row">
<div class="col-sm-4">
<input type="file" id="real-file" name="img1" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button"><i class="fas fa-plus"></i></button>
<span id="custom-text">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file1" name="img2" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button1"><i class="fas fa-plus"></i></button>
<span id="custom-text1">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file2" name="img3" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button2"><i class="fas fa-plus"></i></button>
<span id="custom-text2">Enviar foto (jpg, png)</span>
</div>
</div>
</div>
</div>
<input type="file" id="testfile" name="tesinput" value="">
<span id="custom-text12">Enviar foto (jpg, png)</span>
<div class="form-group ">
<div class="row">
<div class="col-sm-4">
<input type="file" id="real-file3" name="img4" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button3"><i class="fas fa-plus"></i></button>
<span id="custom-text3">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file4" name="img5" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button4"><i class="fas fa-plus"></i></button>
<span id="custom-text4">Enviar foto (jpg, png)</span>
</div>
</div>
<div class="col-sm-4">
<input type="file" id="real-file5" name="img6" hidden="hidden" />
<div class="upload_btn_style">
<button type="button" id="custom-button5"><i class="fas fa-plus"></i></button>
<span id="custom-text5">Enviar foto (jpg, png)</span>
</div>
</div>
</div>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg btn-block submit " value="Enviar " type="submit" name="button_submit" id="button_submit">
</div>
<!-- to be change Hidden value added for WordPress ajax security and to validate function name -->
<input type="hidden" name="action" value="field_action_for_ajax_tratamento"/>
<div class="msgs-formulario"></div>
<!-- Start success message -->
<div class="alert alert-success" id="success_message" role="alert">Mensagem enviada com sucesso <i class="far fa-thumbs-up"></i> Aguarde o nosso retorno!
</div>
</form>
function contact_ajax_tratamento() {
// This calls the javascript we just created for the form validation
wp_localize_script( 'tratamento_ajax', 'wp_ajax', array(
'tratamento' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( "ajax_nonce" ) // this is a unique token to prevent form hijacking
) );
// Enable any admin to run ajax_login() in AJAX and POST the form
add_action( 'wp_ajax_field_action_for_ajax_tratamento', 'ajax_tratamento_form_process' );
// Enable any user with no privileges to run ajax_login() in AJAX and POST the form
add_action( 'wp_ajax_nopriv_field_action_for_ajax_tratamento', 'ajax_tratamento_form_process' );
}
// Initiate the ajax enquiry form and add the validation javascript file
add_action( 'init', 'contact_ajax_tratamento' );
add_action( 'wp_enqueue_scripts', 'contact_ajax_tratamento' );
// Function to send the email using the email template
function ajax_tratamento_form_process() {
ob_start();
// include THE MAIL TEMPLATE
include('assets/template/tratamento.php');
$message = ob_get_contents();
ob_end_clean();
// HARD CODE IMAGE
// $atachments = array( get_home_path() . '/wp-content/uploads/2020/03/Wedding.jpg');
$uploadedfile = $_FILES['image'];
$upload_overrides = array( "test_form" => false );
$uploaded_file = wp_handle_upload ($file, $upload_overrides);
if( isset( $uploaded_file ["image"] )) {
$file_name_and_location = $uploaded_file ["image"];
$file_title_for_media_library = $title;
$attachment = array(
"post_mime_type" => $uploaded_file_type,
"post_title" => addslashes( $file_title_for_media_library ),
"post_content" => "",
"post_status" => "inherit"
);
if( ! is_null( $post )) {
if ( ! is_numeric( $post )) {
$post = $post->ID;
} // if ()
$attachment ['post_parent'] = $post;
} // if ()
$id = wp_insert_attachment( $attachment, $file_name_and_location );
require_once( ABSPATH."wp-admin/includes/image.php" );
$attach_data = wp_generate_attachment_metadata( $id, $file_name_and_location );
wp_update_attachment_metadata( $id, $attach_data );
} // if ()
// $headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: ' . trim( $_POST[ 'name' ] ) . ' <' . trim( $_POST[ 'email' ] ) . '>' . "\r\n";
wp_mail('example#example.com', "FORMULARIO TRATAMENTO - SUBJECT", $message, $headers, $atachments);
$results['feedbacks'] = "All OK!";
wp_send_json($results);
die();
}
I have a website that has a blog and a custom post type called proyecto.
They are on mywebpage.com/blog and mywebpage.com/portafolio.Each of those pages have a search field and category dropdown selector.
Each page should only search and filter for their respective posts. Also, each of them has a custom search result layout. If I'm on mywebpage.com/portafolio the search results should only display proyecto post type results with its custom layout. The same goes for when I only select a category from the dropdown (and dont type anything), it should only display posts type proyecto that have the category selected and only show the categories assigned to proyecto post types, again, with its own layout.
At the moment the search and category filter work (Im not even sure if it works properly) but it merges both of the results (or doesnt show them all) and only displays them with the post design layout... and when I look for an empty seach,it always displays all of the proyecto posts and some of the post results with the proyecto layout.
Here is my code on both page-blog.php and page-proyecto.php:
<section id="search">
<div class="container">
<div class="page-title">PAGE TITLE</div>
<form action="" method="post" class="search-form searchandfilter justify-content-between" id="searchbar">
<div class="row">
<div class="offset-lg-2 col-lg-6">
<label class="search-bar">
<div class="btn-search"><i class="fa fa-search"></i></div>
<input type="search" name="ofsearch" class="search-txt" placeholder="<?php echo esc_attr_x( 'Búsqueda', 'placeholder' ) ?>" value="<?php echo get_search_query()?>" />
<input type="hidden" name="post_type" value="post_type"/>
</label>
</div>
<div class="col-lg-4">
<?php
$categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) );
if( $terms = get_categories( array('taxonomy' => 'category', 'orderby' => 'name') ) )
{
?>
<select name="ofcategory" id="ofcategory" class="postform custom-select" name="categoryfilter">
<option value="0">Categoría</option>
<?php
foreach ( $terms as $term )
{
?>
<option value="<?php echo $term->term_id;?>" data-categoria="<?php echo $term->name;?>"><?php echo $term->name;?></option>
<?php
}
?>
</select>
<?php
}
?>
<input type="hidden" name="ofcategory_operator" value="and">
</div>
</div>
<div class="row justify-content-end">
<input type="hidden" name="ofsubmitted" value="1">
<input type="submit" value="Filtrar" class="btn-submit btn-main-aqua">
<div class="btn-submit btn-main-aqua" onclick="displaymessage();reset()">Vaciar Campos</div>
</div>
</form>
</div>
</section>
NOTE:
This is the same code I use on both pages.
I only change the page title and value of the hidden input in the search.
I cannot know what the categories' names will be, they will be created by the user.
Here is my code on both search.php and category.php:
<?php get_header();?>
<section id="search">
<div class="container">
<div class="row m-0"><div class="page-title">Resultados de búsqueda</div></div>
<div class="row p-3"><div class="title">"<?php the_search_query();?>"</div></div>
<div class="row row-eq-height m-0">
<?php
if(have_posts())
{
if (get_post_type() == 'proyecto')
{
while (have_posts())
{
the_post();
$img = get_field("port-logo");
?>
<div class="col-lg-4 col-md-6 col-sm-12 p-0" data-aos="fade-up" data-aos-duration="1000">
<a href="<?php the_permalink();?>">
<div class="proyecto-img">
<img src="<?php echo $img;?>">
</div>
</a>
</div><!-- ITEM -->
<?php
}
}
else
{
if (get_post_type() == 'post')
{
while (have_posts())
{
the_post();
$titulo = get_field("blog-titulo");
$subtitulo = get_field("blog-subtitle");
$img = get_field("blog-img");
$intro = get_field("blog-intro");
?>
<div class="col-lg-4 col-md-6 col-sm-12" data-aos="fade-up" data-aos-duration="1000">
<div class="post-cont">
<a href="<?php echo the_permalink();?>">
<div class="post-img">
<img src="<?php echo $img;?>">
<div class="subtitle"><?php foreach((get_the_category()) as $category){ echo '- ' . $category->cat_name; }?></div>
</div>
</a>
<div class="desc-title"><?php echo the_title();?></div>
<div class="post-desc"><?php echo $intro;?></div>
Leer más
</div>
</div><!-- ITEM -->
<?php
}
}
else
{
?>
No results.
<?php
}
}
}
else
{
?>
No results.
<?php
}
?>
</div>
<div class="row p-0 m-0 mt-5">
<div class="btn-main-red">Regresar</div>
</div>
</div>
</section>
<?php get_footer();?>
Im not sure if this is the correct way to code it or if I should have 2 different search results documents for that...
My main problem is that they function as if they were 1 same search form instead of 2 different (because that code is on each page with different values on the hidden field), same goes to the search.php. Should I make 2 different files, if so how should i o it?
When I filter by category, I would like the url outcome to be something like this:
for the custom post type: mywebpage.com/proyecto/category/category1/?s=some_text
and for blog: mywebpage.com/category/category1/?s=some_text
I also tried using POST and GET and it did work but it makes my categories stop working...
I have been looking arround for an answer to my problem for days and even searched in the codex and various forums but I have not been successful, Im also a begginer so I dont know if I may have found the answer but I just didnt understand it... Any form of help adn guidance is apreciated, thanks.
Ok, here i can give you a clear idea that how to process... Firstly create a custom taxonomy for your CPT proyecto.. Eg proyecto_cat Create Taxonomy
Search Form Code
<form method="post" action="<?php echo site_url('search-results'); ?>">
<div class="row">
<select name="proyecto_cat">
<?php
$proyecto_cat = get_terms( array(
'taxonomy' => 'proyecto_cat',
'hide_empty' => false,
'orderby' => 'name'
) );
foreach ($proyecto_cat as $key => $value) {
?>
<option value="<?php echo $value->term_id; ?>"><?php echo $value->name; ?></option>
<?php } ?>
</select>
<input type="submit" name="proyecto_submit" value="Search" class="proyecto_submit">
</div>
</form>
Then Create a search-results.php template file and assign it to the page as we have mentioned in form action url.
search-results.php file
<?php
$proyecto_cat = $_POST['proyecto_cat'];
$tax_query = array(
array(
'taxonomy' => 'proyecto_cat',
'terms' => $proyecto_cat,
'field' => 'term_id',
'operator' => 'IN',
),
);
$args = array(
'post_type' => array( 'proyecto' ),
'order' => 'DESC',
'posts_per_page' => '-1',
'orderby' => 'date',
'tax_query' => $tax_query,
);
$query = new WP_Query( $args );
print_r($query);
I'm making a plugin and I don't understand why my shortcode doesn't work if I insert into a woocommerce product page.
It does work if I add it elsewhere on another page, just not on product pages.
The shortcode is added very simply:
add_shortcode('testing', 'testingform');
function testingform() {
ob_start();
echo 'Width:<br>
<input type="text" name="width" id="width"><br>
Height:<br>
<input type="text" name="height" id="height"><br>
Scale:<br>
<input type="text" name="scale" id="scale"><br><br>
<input type="submit" name="submit" id="submitbtn"><br>
<button type="button" id="stripbtn">Show Strips</button>
<div class="wallpapercontainer">
<div class="holder">
<div class="wallpaper">
</div>
</div>
<div class="heightbar"></div>
<div class="widthbar"></div>
</div>';
$output = ob_get_contents();
ob_end_clean();
return $output;
}
I use this hook:
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_field' );
function add_custom_field( $checkout ) {
echo '<div id="custom_field_wrapper">';
woocommerce_form_field( 'custom_field', array(
'type' => 'text',
'class' => array('custom_field_class'),
'label' => __('Custom Field'),
'placeholder' => __('Please input value ...'),
'required' => false, ),
$checkout->get_value( 'custom_field' ) );
echo '</div>';
}
and i get this markup:
<div id="custom_field_wrapper">
<p class="form-row custom_field_class" id="custom_field_field" data-priority="">
<label for="custom_field" class="">Custom Field</label>
<input type="text" class="input-text " name="custom_field" id="custom_field" placeholder="Please input value ..." value="">
</p>
</div>
But the required markup looks like this:
<div class="row order-form__row">
<div class="col-lg-3 col-md-4">
<label class="label" for="index-field">Индекс:</label>
</div>
<div class="col-lg-6 col-md-6">
<input class="input" type="text" name="index" id="index-field"/>
</div>
<div class="col-lg-3 col-md-2"><a class="field-after" href="javascript:void(0);" target="_blank">Забыли индекс?</a></div>
</div>
Kind smart people help solve the dilemma ... How to change the html markup according to the requirements?
I want my wordpress plugin to redirect to custom page, please refer below page. please refer coding given below
login page after login i want them to move to speific url.
I already try few plugin, all never done the job. I used : https://wordpress.org/plugins/redirect-after-login/.
So please help me to edit some coding in wordpress.
Please refer Login/Register Page Template (template-register.php)
<?php
/* Template Name: Login/Register */
get_header();
/* Site Showcase */
imic_page_banner($pageID = get_the_ID());
/* End Site Showcase */
/* Login/Register Page Design Layout
============================================*/
$pageLayout = get_post_meta(get_the_ID(),'imic_register_layout',true);
$contentClass = 4;
if ($pageLayout != 1) { $contentClass = 6; }?>
<!-- Start Content -->
<div class="main" role="main"><div id="content" class="content full"><div class="container">
<div class="page"><div class="row">
<?php
echo '<div class="col-md-'.$contentClass.' col-sm-'.$contentClass.'">';
/* Page Content
======================*/
while (have_posts()):the_post();
the_content();
endwhile;
echo '</div>';
/* Manage Login Form
========================*/
if ($pageLayout == 1 || $pageLayout == 2) { ?>
<div class="col-md-4 col-sm-4 login-form">
<h3><?php _e('Login','framework'); ?></h3>
<form id="login" action="login" method="post">
<?php
$redirect_login= get_post_meta(get_the_ID(),'imic_login_redirect_options',true);
$redirect_login=!empty($redirect_login)?$redirect_login: home_url();
?>
<input type ="hidden" class ="redirect_login" name ="redirect_login" value ="<?php echo $redirect_login ?>"/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input class="form-control input1" id="loginname" type="text" name="loginname">
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input class="form-control input1" id="password" type="password" name="password">
</div>
<div class="checkbox">
<input type="checkbox" checked="checked" value="true" name="rememberme" id="rememberme" class="checkbox"> <?php _e('Remember Me!','framework'); ?>
<?php echo ''.__('Forget Password','framework').''; ?>
</div>
<input class="submit_button btn btn-primary button2" type="submit" value="<?php _e('Login Now','framework'); ?>" name="submit">
<?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?><p class="status"></p>
</form>
</div>
<?php } ?>
</div></div></div></div></div>
<?php get_footer(); ?>
No need to create form in WordPress for login. WordPress provide functionality for this.
You can use wp_login_form($args) function for login.
Put this code in your template file.
$redirect_url = get_site_url();
$args = array(
'echo' => true,
'redirect' => $redirect_url,
'form_id' => 'loginform',
'label_username' => __('Email Address'),
'label_password' => __('Password'),
'label_remember' => __('Remember Me'),
'label_log_in' => __('Log In'),
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
'remember' => true,
'value_username' => NULL,
'value_remember' => false);
wp_login_form($args);
I hope this will work for yor.
There is a function in wordpress for redirection. Follow this link https://codex.wordpress.org/Function_Reference/wp_redirect.
User this function before get_header() statement.
Hope it will help.