Posts

Showing posts with the label POST

Arrange posts by "order" attribute in WordPress

It's just simple as this function: function my_get_posts( $query ) {     if ( is_home() && false == $query->query_vars['suppress_filters'] )                 $query->set('orderby', 'menu_order');                 $query->set('order', 'ASC');      return $query; } Basically, you need to order the posts by menu_order attribute.

Get last published posts in your entire WordPress Multisite network

To display last published posts from the entire network on the main blog of your WordPress Multisite, you can use this plugin I wrote (*) yesterday: Github repo:   https://github.com/dangtrinh/WordPress_Multisites_Utilities_Plugin 1. Install the WordPress Multisite Utilities Plugin: $ cd /var/www/your_wordpress_installation/wp-content/plugins $ git clone https://github.com/dangtrinh/WordPress_Multisites_Utilities_Plugin 2. Activate the plugin for your network (network only) 3. In your home page template file (for example page-home.php), call the  get_recent_network_posts function to get latest posts (6 posts by default) from your network. For example: <?php $lastposts = get_recent_network_posts(); foreach( $lastposts as $post): ?> <div class="col-1-3"> <div class="blog-post"> <div class="blog-post-thumbnail" style="background-image: url('<?php echo $post['thumb_url'];?>')"...

How to be able to send form data with the '@' character at the beginning of the data string in PHP cURL

If you post form data using cURL in php, you cannot use the ' @ ' character at the beginning of a data string value (e.g: password=@@mypassword ). ' @ ' is the indicator for file path in POST operation of cURL. You need to disable the '@' prefix to be able to send data string start with '@' character. Here is the description of PHP cURL's CURLOPT_POSTFIELDS option: CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to...

Django Form - A shorter way to work with form

I've just found a shorter way to work with Django form in a view with POST request. Instead of: def home (request): if request.method == 'POST': myform = MyForm(request.POST, request.FILES) if myform.is_valid(): myform.save() return redirect('home') else: myform = MyForm() return render_to_response('home.html', locals(), context_instance=RequestContext(request)) I can do something like this: def home (request): myform = MyForm(request.POST or None) if myform.is_valid(): myform.save() return redirect('home') return render_to_response('home.html', locals(), context_instance=RequestContext(request))

Django - Check variables in POST/GET request

When a form is submitted, I wanna get the value of a key (variable) which is in the POST/GET data. For example: myvalue = request.POST['mykey'] But if for some reason, the POST/GET data does not contains that data ('mykey': 'myvalue'). In this case, I have to check if the key 'mykey' is sent in POST/GET data and check if that key is not empty: if 'mykey' in request.POST.keys() and request.POST['mykey']:       myvalue = request.POST['mykey'] There is a Python method available on any Python dictionary names  get() . get() lets you ask for the value of a particular key and specify a default to fallback on if the key doesn't exist. And, POST and GET are dictionaries. myvalue = request.POST.get('mykey', '') That get() method will save me (and you) so much time!