Articles of php

How to use wp_localize_script in a WordPress page other than functions.php?

I have a contact form in a WordPress page like this: $( ‘#contact_form’ ).bootstrapValidator({ fields: { // … }, submitHandler: function( formInstance ) { $.post( “../send-message”, $(“#contact_form”).serialize(), function( result ) { alert( pw_script_vars.State ); }); } }); When the form is submitted using the Ajax request it goes to another WordPress page titled /send-message that […]

How do I get URL from WP_HTTP object?

I’m using wp_remote_post: $test = wp_remote_post($environment_url, $args2 ); $test2 = $test[‘http_response’]; So what I want to do is get the value of URL but I believe the object is protected so having difficulty getting to it. The answer may be here WP_HTTP_Requests_Response But I need help finding it. Here is the output of $test2: WP_HTTP_Requests_Response […]

Pass PHP variable to JavaScript without inline JS

So everybody and their mother says to use wp_localize_script to pass PHP variables client side. Even though the function is not actually made for this purpose, everybody does it. Kind of like Q-Tips: not made for cleaning ears, everybody does it anyways. With technology moving forward and people generally against things inline I don’t want […]

Insert array data on plugin activation

I am working on custom plugin. I need to insert currency names and symbols in table on plugin activation. Right now my database insertion code is : global $wpdb; $table_name1 = $wpdb->prefix . “woo_currency”; $curr_name = array(‘Dollars’, ‘Ruble’, ‘Riel’); $curr_symbol = array(‘$’, ‘p.’, ‘៛’); $insert = $wpdb->insert($table_name1, array( ‘name’ => $curr_name, ‘symbol’ => $curr_symbol, ),array(‘%s’, […]

Attaching a pdf to Contact Form 7 e-mail via functions.php

I’m trying to attach pdf files to an e-mail manually and I did some research on the topic and found this code: add_action(‘wpcf7_before_send_mail’,’send_pdf’); function send_pdf( $cf7 ) { $id = $cf7->id(); if ($id==2399){ $submission = WPCF7_Submission::get_instance(); $submission->add_uploaded_file(‘pdf’, get_template_directory().’/pdf/test.pdf’); } } The strange thing is that after the email is sent, the file and the folder […]

doing an ajax request always outputs 0

I’m trying to do an ajax request from my themes header.php file. I’ve read that I have to use the admin-ajax.php file to achieve this. So I looked for examples and found this one. My code in the header.php file looks like this: <script> $(document).ready(function(){ $(‘.test a’).click(function(){ var className = $(this).attr(‘class’); $.ajax({ url: “<?php echo […]

Is there a built in function to see if a URLis oEmbed Compatible?

I want to be able to take a url and see if the domain is one of the ones WordPress supports to add embeds via oEmbed. Is there a built in function that does this in WordPress or would I need to create my own? Example: if I have a url from a video site […]

Creating login session via CURL

Using the following code I experienced some problems: Login works but every link in the dashboard I will click will follow into the 404 not found page. It seems that there won’t be a working session created? $username=”admin”; $password=”admin”; $url=””; $cookie=”cookie.txt”; $postdata = “log=”. $username .”&pwd=”. $password .”&wp-submit=Log%20In&redirect_to=”. $url .”wp-admin/&testcookie=1″; $ch = curl_init(); curl_setopt ($ch, […]

WordPress shows registration link for non logged users

I have a WordPress site and I want to add a registration link for visitors. So, I added a menu item called ‘REGISTER’ with the following link: The problem is that this link opens the registration form for both users and visitors and I want to show it only to visitors.

How do I hardcode a WordPress shortcode into my theme?

I am using WooTheme’s Canvas theme and would like to “hardcode” in the [post_comments] shortcode into my theme. [post_comments] displays a comment link with comment count (ie Comments 10) in my post. Right now I am inserting the [post_comments] shortcode into my theme’s meta “Meta Manager”; however, this is not ideal for me as I […]