Elementor breaks site with custom shortcode - wordpress

I created a custom shortcode which includes a special loop which includes all posts from different multisite blogs. This solution is provided by this plugin: https://rudrastyh.com/. The shortcode is perfectly working an all normal pages and posts.
But I am also using the page builder Elementor. When inserting this shortcode into Elementor some strage things are happening: in editor mode the shortcode output is showing up twice, once at the top of the editor area and once again at the place where I actually put the shortcode. When I hit save, my whole site breaks and shows a standard image when accesing any page. Then the the only solution is to recover my latest database backup.
Here I show you some screenshots of the editor mode:
Here my shortcode fuction:
// Add Shortcode
function all_events_shortcode ($atts) {
// Attributes
$atts = shortcode_atts(
array(
'lang' => '',
'blog' => '',
),
$atts
);
// Network_Query parameters
$args = array(
'posts_per_page' => 14,
'blog_id' => esc_attr($atts ['blog']),
'lang' => esc_attr($atts ['lang']),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_type' => 'noo_event',
'meta_key' => '_noo_event_start_date',
'meta_value' => date( "U" ),
'meta_compare' => '>'
);
$network_q = new Network_Query( $args );
// if there are posts, then print <ul>
if( $network_q->have_posts() ) :
echo '<div id="all_events">';
// run the loop
while( $network_q->have_posts() ) : $network_q->the_post();
// the get_permalink() function won't work without switch_to_blog()
// you can use network_get_permalink() instead but it is a little slower
switch_to_blog( $network_q->post->BLOG_ID );
// Get the dates
$start_date=get_post_meta($network_q->post->ID, '_noo_event_start_date', true);
$_start_date = gmdate("d.m.Y", $start_date);
$end_date=get_post_meta($network_q->post->ID, '_noo_event_end_date', true);
$_end_date = gmdate("d.m.Y", $end_date);
// you can obtain the post title from $network_q->post object
echo '<div class="all_events_item post-' . $network_q->post->ID . ' blog-' . $network_q->post->BLOG_ID . '">
<div class="all_events_img">
<a href="' . get_permalink( $network_q->post->ID ) . '">
'.get_the_post_thumbnail( $network_q->post->ID, 'large' ).'
</a>
</div>
<div class="all_events_content">
<h2>' . $network_q->post->post_title . '</h2>
<br />
<span class="start_date">'.$_start_date.'</span> -
<span class="end_date">'.$_end_date.'</span>
</div>
</div>';
// restore_current_blog() to switch to the previous (!) website
restore_current_blog();
endwhile;
echo '</div>';
endif;
network_reset_postdata(); // add it after the loop if you plan to use Network_Query multiple times on the page
}
add_shortcode('all-events', 'all_events_shortcode');
Can you give me some hints how I could tackle this problem?
Best wishes

Try this, it helped me.
Adding ob_start(); and then ob_get_clean();
function function_name(){
ob_start();
//..your code here...
$content = ob_get_clean();
return $content;
}
add_shortcode('shortcode_name','function_name');
Found the solution here and tried it. It worked for me.
Elementor page builder shortcode issue
Original credit: https://stackoverflow.com/a/48813883/9364624
Posted By: https://stackoverflow.com/users/1753934/colin-oakes

You need to bind HTML in variable and then return this HTML from shortcode..
Please check the code below
function _login_popup() {
$html = '<form id="user-login-form" method="post" action="#" class="js-form-redirect js-form-action" novalidate="">
<div class="floating-label form-group">
<input id="useremail" name="user_name" required="" value="" type="email">
<label for="useremail">Email</label>
</div>
<div class="floating-label form-group">
<input id="userpassword" name="password" required="" value="" type="password">
<label for="userpassword">Password</label>
</div>
<div class="o-form__buttons text-right --small-center">
<button type="submit" id="submit_login" class="a-button-form --save a-no-before" value="edit">Sign in</button>
</div>
</form>';
return $html;
}
add_shortcode('yg-login-popup', '_login_popup');
In this shotcode i have created login form..

Related

page content function for wp_insert_post

I want to add two text fields of username and password in the page content. Basically i am making my plugin. I have made a page which is connected to my plugin. I want to add content in this page by using function.
my page code is:
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function() {
$post = array(
'page_template' => '',
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'Checklists',
'post_status' => 'publish' ,
'post_title' => 'Checklists',
'post_type' => 'page',
'post_content' => 'my_function()'
);//insert page and save the id
$newvalue = wp_insert_post( $post, false ); //save the id in the database
update_option( 'hclpage', $newvalue );
}
my_function()
{
// A login form will be here.
}
Is it possible ??? what will be the best way to do it in the plugin file of wordpress ???
To add content I did not use function. But I use php variable.
$form = '<form action="..../wp-content/plugins/wp-link-with-parse/php-sdk/test.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<div id="lower">
<input type="submit" name="login" value="Login">
</form>';
to assign content use:
'post_content'=> $form
i know this post is from 5 years ago, but i'm currently trying to do a wordpress plugin, and i found a solution for adding content to a custom page in wordpress, what i did is to create a .php file with all the content, including php code, and then include that code in my post_type page by creating a variable and adding that file to that variable with file_get_contents, something like this:
enter image description here
hope it can be useful

Formatting buttons in a form (CodeIgniter)

Just wondering how to go about giving a form in CodeIgniter a class? I've tried just formatting buttons, hoping that the submit button would change in the form, but it didn't.
echo form_open('User/feed' class='buttonClass');
echo form_submit('NF', 'News Feed');
echo form_close();
I couldn't find anything which seemed to help me online.
Thank you!
echo form_open('User/feed', array( 'class' => 'classname' ));
// will become:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/User/feed" class="classname" />
echo form_submit('NF', 'News Feed');
// will become:
<input type="submit" name="NF" value="News Feed" />
echo form_close();
// will become:
</form></div></div>
Now keep in mind, adding classes and other attributes via the array in the first line up there only adds them to the Form line. I would recomend, if you're doing this in view, writing pure html and adding in the information needed. More like:
<form method="post" accept-charset="utf-8" action="<?= base_url('User/feed'); ?>" class="classname">
<div>
Something here for the form
<input type="text" name="stuff" />
</div>
<input type="submit" name="NF" value="News Feed" class="myButton" />
</form>
Also, of note, you can create buttons using an array and assign class and other attributes that way. Such as:
$myButton = array(
'class' => 'myButton',
'name' => 'NF',
'value' => 'News Feed',
);
echo form_button($myButton);
Lastly, and I think this is what you're aiming for, you can do the same with form_submit:
$myButton = array(
'class' => 'mySubmitButton',
'name' => 'nfSubmit',
'value' => 'Submit',
);
echo form_submit($data);
Per the documentation, the form helper accepts an array as the second parameter, allowing one to apply various options, such as a class. For example:
$attributes = array(
'class'=>'myClass'
);
echo form_open('User/feed', $attributes);
Documentation
Form Helper - http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

wp_insert_post not adding category

I'm building a WordPress theme where people can submit posts using wp_insert_post. The code below adds the post title but does not add the category specified by the user. Instead it puts it in uncategorized.
How do I get it to add the new category to the post when submitted?
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$new_cat_ID = $_POST['category'];
//Checking if category already there
$cat_ID = get_cat_ID( $_POST['newcat'] );
//If not create new category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $_POST['newcat']);
wp_insert_category($cat_name);
}
//Get ID of newly created category
$new_cat_ID = get_cat_ID($_POST['newcat']);
// Create post object
$new_post = array(
'ID' => '',
'post_title' => $post_title,
'post_status' => 'publish',
'post_author' => $user->ID,
'tax_input' => array( 'category' => $new_cat_ID )
);
// Insert the post into the database
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( home_url() );
exit;
}
Here's the HTML for the form:
<form style="" action="" method="post" id="foo">
<input type="hidden" name="new_post" value="1"/>
<input type="text" name="post_title" value="title" id="input-title"/>
<input type="text" name="category" value="apples" id="category" />
<input type="submit" value="Login">
</form>
The category input should be:
<input type="text" name="newcategory" value="apples" id="category" />
In my tests, wp_insert_category did not work and wp_insert_term had to be used (as per this forum thread).
Your wp_redirect is not taking where you thing it does. The section //This will redirect you to the newly created post is plain wrong.
The following is a working example with a security layer added with wp_nonce_field, but you must add User Input Data Validation.
Also, I'm testing while logged in, so it works. Your code does not take care of $user->ID, research for get_current_user to get this right.
<?php
if ( isset( $_POST['noncename_so_17539370'] ) && wp_verify_nonce( $_POST['noncename_so_17539370'], 'nonce_so_17539370' ) )
{
if( isset($_POST['new_post']) == '1' ) {
//Checking if category already there
$cat_ID = get_cat_ID( $_POST['newcat'] );
//If not create new category
if( !$cat_ID ) {
$arg = array( 'description' => "my description", 'parent' => 0 );
$cat_ID = wp_insert_term($_POST['newcat'], "category", $arg);
}
// Create post object
$new_post = array(
'ID' => '',
'post_title' => $_POST['post_title'],
'post_status' => 'publish',
//'post_author' => $user->ID,
'tax_input' => array( 'category' => $cat_ID )
);
// Insert the post into the database
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = ;
wp_redirect( get_permalink( $post_id ) );
exit;
}
} else {
echo 'ERROR';
}
?>
<form style="" action="" method="post" id="foo">
<?php wp_nonce_field( 'nonce_so_17539370', 'noncename_so_17539370' ); ?>
<input type="hidden" name="new_post" value="1"/>
<input type="text" name="post_title" value="title" id="input-title"/>
<input type="text" name="newcat" value="apples" id="category" />
<input type="submit" value="Login">
</form>

wp insert post with my code on a blank new post

i am using this code to create a new page when my plugin is activated:
function create_policy() {
$my_post = array(
'post_title' => 'mobile page',
'post_content' => 'this is my content',
'comment_status' => 'closed',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 3,4 )
);
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
what i want to do is :
1) use my own code in the 'post_content', not regular text.
2) clear all the page and leave only the GET_header
i want a blank page with my code.
any advice ?
in addition to the comments:
this is the admin pannel code:
<?php
// create custom plugin settings menu
add_action('admin_menu', 'zeev_create_menu');
function zeev_create_menu() {
//create new top-level menu
add_menu_page('zeev movile redirect', 'zeev mobile', 'administrator', __FILE__, 'zeev_settings_page', 'favicon.ico');
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
//register our settings
register_setting( 'zeev-settings-group', 'zeev_tracking_code' );
}
function zeev_settings_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields('zeev-settings-group'); ?>
<table class="form-table">
<tr valign="top">
<td><textarea name="zeev_tracking_code" cols="90"><?php echo get_option('zeev_tracking_code'); ?></textarea></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
and this is the veriable what need to find himself on a blank page someware (and to work)
<?php echo get_option('zeev_tracking_code'); ?>
There are a few ways to get custom layouts in Wordpress on a page-by-page basis, but the most common is to create a new, custom template. In the theme folder, create a new file named what you like:
my-custom-template.php
The easiest way to start is to copy what you find in page.php into that directory. That will give you the structure with the headers and footers which are useful. Right at the top of the file, put:
<?php
/*
*
* Template Name: [My template]
*
*/
?>
Doing this will tell WP to automatically make this template an option when creating a new page. Now, when you create a page, you can select this template from the dropdown, and the page will use that. Inside the template itself, you can put whatever PHP or HTML code you need inbetween the get_header() and get_footer() you like, to style it, or just to show your two buttons.
If you want a totally blank page, you can remove the get_header() and get_footer() altogether.

Public Post Form for WordPress

Hi I'm looking for a very simple quick (no-plugins though) way to add a form inside a page template that will allow a logged-in user to post from outside WP-Admin. So basically the post will be under the logged-in author.
I have been looking around the web for some tutorials etc but haven't had too much luck and many seem to want to opt for plugins etc.
Can anyone help thanks.
You can use the wp_insert_post() function.
take a look at this. you can paste it in category.php for example and visit a category page and check it. but make sure to put the top code above the get_header() function.
<?php
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = $_POST['cat'];
$post_content = $_POST['post_content'];
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_category' => array($post_category),
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>
--
<!-- this form shows only if user is logged in -->
<?php if ( is_user_logged_in() ) { ?>
<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
<?php } ?>
for more info you should take a look here wp_insert_post with a form
good luck
If you don't mind changing your theme, you might want to try P2: http://wordpress.org/extend/themes/p2
There's also a video of it in action here: http://p2theme.com/
P2 allows people to post straight from the home page, which seems like what your looking for.

Resources