Formatting buttons in a form (CodeIgniter) - css

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

Related

Add Field To Custom WP Login Form

I have a custom login form that I display on a page using the following:
$my_login_args = apply_filters( 'my_login_page_args', array(
'echo' => true,
'redirect' => site_url( $_SERVER['REQUEST_URI'] ),
'form_id' => 'my_login_form',
'label_username' => esc_html__( 'Email Address' ),
'label_password' => esc_html__( 'Password' ),
'label_remember' => esc_html__( 'Remember Me' ),
'label_log_in' => esc_html__( 'Sign 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( $my_login_args ); ?>
I need to pass a custom variable ($my_custom_id) stored in the login page through to the redirected page a user sees after login. Is there any way to append the URL with this variable using POST or GET or is the best solution to add this as a hidden field in the form?
Also, in terms of adding an extra field to the form I've tried adding a custom input box to the form using
add_action('login_form','my_added_login_field');
function my_added_login_field(){
//Output your HTML
?>
<p>
<label for="my_extra_field">My extra field<br>
<input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
</p>
<?php
}
However this only adds the field to the main Wordpress login and not my custom form. Any thoughts on how best to proceed?
You can you add custom field in login form at top, bottom, and in middle.
I have added login field in login_form_middle.
For Reference https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/general-template.php#L390
https://codex.wordpress.org/Customizing_the_Login_Form
/*
You can use these hooks as well to place your fields
login_form_bottom - login_form_top - login_form_middle
*/
add_filter('login_form_middle','my_added_login_field');
function my_added_login_field(){
//Output your HTML
$additional_field = '<div class="login-custom-field-wrapper"">
<label for="my_extra_field">My extra field<br>
<input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
</div>';
return $additional_field;
}

Remove ids from Wordpress form and form elements

When i use this inside of the loop on my bloglist page:
<?php comment_form( array( 'post_id' => get_the_ID() ) ); ?>
I get a form for each post wich is exactly what I want.
My problem is that since the form itself and the input fields and textarea has ids, the page won't validate. I get errors for multiple ids naturally.
For example does all my forms have id="commentform"
How can I make Wordpress remove all ids in these forms?
You can pass more options to the function see below. Replace <INSERT-UNIQUE-VALUE> with something unique or you can auto generate it with php for example md5(time());
<?php
// Specify options
$options = array(
'id_form' => '<INSERT-UNIQUE-VALUE>',
'id_submit' => '<INSERT-UNIQUE-VALUE>',
'comment_field' => '<p class="comment-form-comment"><label for="<INSERT-UNIQUE-VALUE>">' . _x( 'Comment', 'noun' ) . '</label><textarea id="<INSERT-UNIQUE-VALUE>" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'
);
// output the form
comment_form($options, get_the_ID());
?>
For more references see: http://codex.wordpress.org/Function_Reference/comment_form#.24args
You could solve this with jquery by replacing your ID with a new ID and adding a number to your ids so the form id's would be id="newId0", id="newId1", id="newId2 and so on. Then it will validate.
$('form').each(function(index){
$(this).attr("id","newId"+index);
});
Here's a jsfiddle

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

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.

Elementor breaks site with custom shortcode

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..

Resources