Vheissu / acf-country-field

A country, city and state field for Advanced Custom Fields v4 & ACF Pro
GNU General Public License v2.0
36 stars 11 forks source link

I am getting error while activating the plugin #9

Open virtual000 opened 8 years ago

virtual000 commented 8 years ago

Hi , Please help me through this error

Parse error: syntax error, unexpected 'add_action' (T_STRING), expecting function (T_FUNCTION) in /home/virtualqube/public_html/anna/wp-content/plugins/acf-country-field-master/acf-COUNTRY_FIELD-v4.php on line 281

I have been using http://www.advancedcustomfields.com/ plugin for creating plugin

fab01 commented 8 years ago

Hi, I had the same issue and I found the error is related to a bug since it's not possible to invoke add_action() hook within a class.

To fix the issue edit the file: acf-COUNTRY_FIELD-v4.php

remove all add_action() lines: add_action('wp_ajax_get_country_cities'.... add_action('wp_ajax_get_us_states'....

change new acf_field_COUNTRY_FIELD(); to $acfV4 = new acf_field_COUNTRY_FIELD();

then in acf-COUNTRY_FIELD.php change: // ACF version 4 function register_fields_COUNTRY_FIELD() { include_once('acf-COUNTRY_FIELD-v4.php'); }

to:

// ACF version 4 function register_fields_COUNTRY_FIELD() { include_once('acf-COUNTRY_FIELD-v4.php'); } add_action('wp_ajax_get_country_cities', array($acfV4, 'get_country_cities')); add_action('wp_ajax_get_us_states', array($acfV4, 'get_us_states'));

Hope it helps ;-)

spoonyto commented 8 years ago

@fab01 Thank a lot for you for your advice!

But I have another problem. If I choose country, it displayed to me waiting status animation and there nothing happened.

Do you know, what it could be?

fab01 commented 8 years ago

@spoonyto Hi, you are very welcome. After struggling with these issues i decided to change radically direction. Especially because ACF plugin doesn't provide for 'Front-end' functionalities.... It is strictly connected to the WP Admin Panel excluding all the rest!!! I wrote a short how-to here Check it out and let me know if you found it helpful. Here you can find a practical example of what I wrote into the how-to ;) Glad to help you

pochocosta commented 8 years ago

Hi @spoonyto and @fab01 I'm just trying to do the same thing. But I'm very new in the wp word. I'll have a look at your how to. Thanks a lot! We stay in touch. Regards

spoonyto commented 8 years ago

@fab01 Wow, it is really awesome! But... always there is a "but" :) I am really newbe in coding... Well, that's why I have question. For instance. I create a web site for artists. I have added few custom fields and custom posts for additional information of artists and art-studios. So, I want to make possibility for adding new artists by registered users. But in your "how to" I can't understand how I can manually add country/city for current post? The question is - How does it work in back-end? Thank you very much!

fab01 commented 8 years ago

It's not that difficult. I going to reply to you during evening Be patient :)

Concerning the DB for free make sure that the Table structure is like this: id | int(11) country_code | varchar(2) country | varchar(255) state_code | varchar(20) states | varchar(255) city_name_ascii | varchar(255)

Otherwise it'll be necessary to change the business logic in terms of db queries

spoonyto commented 8 years ago

@fab01 Ok, and how it waork in back-end? Will I able to add custom fields in back-end?

fab01 commented 8 years ago

During the weekend I will try to provide an example. In the meanwhile check for add_meta_box() function

spoonyto commented 8 years ago

@fab01 Hi Fab! Did you succeed with example?:)

fab01 commented 8 years ago

Hi @spoonyto sorry for delay, I was a bit busy. Well, I don't know if you had the chance to check the function add_meta_box() as suggested above.

Assuming that you already have your own custom post (example photo) and your own theme (photographers) the steps to include the fieldset within your 'post' admin page are pretty easy:

1) in functions.php create and hook a function:

photographers_theme_setup() { 
  // This will add photo custom fields
  require get_template_directory() . '/inc/photo_meta.php';
}
add_action( 'after_setup_theme', 'photographers_theme_setup' );

2) in photographers/inc/photo_meta.php:

add_action('add_meta_boxes', 'register_photo_settings');
function register_photo_settings() {
// Include the JS file (the same of the tutorial. !!!Important)
wp_enqueue_script( 'countryField', get_template_directory_uri() . '/js/countryField.js', array(), '1.0.0', true );
// photo is the name of your custom post
add_meta_box('photographers_photo_basic_settings', 'Basic Info', 'display_photographers_photo_basic_settings','photo');
}

function display_photographers_photo_basic_settings ($post) { 
?>

    <h3><?php _e( 'Select Country:', 'themesdojo' ); ?></h3>
    <select name="countryCode" id="countryCode" class="countrySelect">
    <?php foreach(populate_country_select() as $key=>$country) { ?>
    <option value="<?php echo $key ?>"><?php echo $country ?></option>
    <?php } ?>
    </select>

    <h3><?php _e( 'Select State/Region:', 'themesdojo' ); ?></h3>
    <select name="stateCode" id="stateCode" class="countrySelect"><select>

    <h3><?php _e( 'Select City:', 'themesdojo' ); ?></h3>
    <select name="cityNameAscii" id="cityNameAscii" class="countrySelect"><select>

<?php
}

Try this. It should be already enough ;) Then of course you have to work around in order to adapt the business logic to your own structure.

spoonyto commented 8 years ago

@fab01 Thank you! But, I can not understand how can I add entire base to my site... It's too tough understand from instructions which were in zip file.

Step 1. Create following table using following command.

CREATE TABLE citydb( id int NULL, country_code nvarchar(2) NULL, country nvarchar(255) NULL, state_code nvarchar(20) NULL, states nvarchar(255) NULL, city_name_ascii nvarchar(255) NULL ) ;

Where I have to type it?

Step 2: Load data into table using following command. Substitute correct path below.

ALTER TABLE citydb CONVERT TO CHARACTER SET utf8 COLLATE utf8_persian_ci; LOAD DATA LOCAL INFILE 'C:/mysqldb.dat' INTO TABLE citydb CHARACTER SET UTF8 fields terminated by '\t' LINES TERMINATED BY '\r\n';

It's should be in file?

Step 3. Verify that the data has been loaded. SELECT count(*) FROM citydb

And this?

fab01 commented 8 years ago

Hi @spoonyto, the base is not that difficult as well but it's something apart from wp. The sql instruction can be performed in different ways... Most common ones are:

The creators of the file and related how-to wrote the steps trying to be as much agnostic as possible because everyone have a different approach with different methodology.

Anyway I suppose you are using phpMyAdmin so, in this case, just open your current wp DB and type within "SQL" Tab textarea the whole command. Be careful to change the table using the table prefix of your current wp DB, example: "wp_" = wp_users, wp_citydb.....

The new table would have been created. At this point it's going to be a bit tricky, you are right.

Step two consists with 2 SQL statements: ALTER TABLE 'prefix'citydb CONVERT TO CHARACTER SET utf8 COLLATE utf8_persian_ci; This one can be performed like the first one from phpMyAdmin.

The second one should be better to be performed via CLI (Command Line Interface). Are you a Mac, a Win or a Linux user? If Win, which kind of environment are you using? EasyPHP, XAMPP? Are you able to use mysql CLI? (if you use a Mac open a terminal and type mysql). If it will ask you to insert username and password then it's ok. We can go on via CLI.

spoonyto commented 8 years ago

@fab01 sorry for stupid questions:)

I have done it. Now it's look like that

2016-03-13_22h35_23

Oh, no... Do I have to add prefix to citydb?

Thank you very much for your assistance!

spoonyto commented 8 years ago

It's really hard to me:( May be I can give access to db and ftp, admin panel?

My skype is spoonyto89

fab01 commented 8 years ago

No no @spoonyto!! absolutely never give credentials to unknown people ___

You did it!!! Simply repeat the procedure but just edit the table name at any occurrence. It's not difficult :)

CREATE TABLE wp_citydb(
id int NULL,
country_code nvarchar(2) NULL,
country nvarchar(255) NULL,
state_code nvarchar(20) NULL,
states nvarchar(255) NULL,
city_name_ascii nvarchar(255) NULL
) ;

ALTER TABLE wp_citydb CONVERT TO CHARACTER SET utf8 COLLATE utf8_persian_ci; LOAD DATA LOCAL INFILE 'your/path/to/mysqldb.dat' INTO TABLE wp_citydb CHARACTER SET UTF8 fields terminated by '\t' LINES TERMINATED BY '\r\n';

SELECT count(*) FROM wp_citydb; This last one is optional. You can directly check via phpMyAdmin panel ;)

spoonyto commented 8 years ago

I've done it!:) But it's strange when I put piece of code to the functions.php and update browser it crashes. I will try to play around it. But if you have any idea, please let me know.

fab01 commented 8 years ago

Does it return a specific Fatal error?

spoonyto commented 8 years ago

Only that from logs

[Wed Apr 20 23:39:17 2016] [error] [client 193.86.114.226] PHP Parse error: syntax error, unexpected '{' in /var/www/mysite/data/www/ebruworld.com/wp-content/themes/mytheme/functions.php on line 298

And I copied that

photographers_theme_setup() { // This will add photo custom fields require get_template_directory() . '/inc/photo_meta.php'; } add_action( 'after_setup_theme', 'photographers_theme_setup' );

I stuck... I don't know what I am doing wrong :(.

Vheissu commented 8 years ago

I will be rewriting this plugin in the coming couple of weeks or so. I moved away from Wordpress and PHP work for a while, but I'll get this fixed up for everyone again.

fab01 commented 8 years ago

Hi @spoonyto It's just a syntax error 'fortunately'... Dumb question: is photographers_theme_setup() { declared as a function? In other words, is it preceded by the keyword function I suppose?

**function** photographers_theme_setup() {

what is exactly the content of row 298?

spoonyto commented 8 years ago

Hi @fab01 No worries at all:) Thank you for your patience Yes i have function ebru_theme_setup() { // This will add photo custom fields require get_template_directory() . '/photo_meta.php'; } add_action( 'after_setup_theme', 'ebru_theme_setup' ); }

Error was in another file, but what I have now https://yadi.sk/i/PaXZ6AsmrNfSM

There are no listed drop down items... Does it mean that DB was imported incorrectly?

This how I imported it https://yadi.sk/i/xB_Gum6YrNfiB

Thank for your help in advance!

fab01 commented 8 years ago

Hi @spoonyto , I can't see the examples you mentioned on the links. Anyway, the db structure as it is (looking at the screenshot you already did) is fine and wouldn't affect on the flow. Since the population of the select fields is the result of 'hidden/delocalized' process filtered by the JSON layer you have to find the error through the output of the js function itself.

Example:

get_cities($(this).val(), function(response) { var obj = JSON.parse(response); var len = obj.length; var $cityValues = ''; alert(response); }); the response should return the error thrown by PHP as a string.

spoonyto commented 8 years ago

Hi @fab01

I found next error PHP Fatal error: Call to undefined function populate_country_select() in /var/www/mysite/data/www/mysite.com/wp-content/themes/mytheme/photo_meta.php on line 14,

Line 14 is: <?php foreach(populate_country_select() as $key=>$country) { ?>

fab01 commented 8 years ago

Hi @spoonyto , very good! I think It's clear enough :) Maybe you missed something on mytheme/functions.php. Check it out and tell me if you have problems ;)

spoonyto commented 8 years ago

Hi @fab01

Yes, it was self-explanatory fact :)

But now, i have this:

[Wed May 04 00:13:23 2016] [error] [client 193.86.114.226] WordPress database error Table 'mysite.wp_citydb' doesn't exist for query SELECT * FROM wp_citydb group by country_code order by country ASC made by include('wp-admin/edit-form-advanced.php'), do_meta_boxes, call_user_func, display_lucid_artist_basic_settings, populate_country_select, referer: http://www.mysite.com/wp-admin/edit.php?post_type=artist

image

but at least I have view like that:)

image

I copied your code from your post.

Is it because of prefix before database?

fab01 commented 8 years ago

Hi @spoonyto, you have to rename the table 'mysite.citydb' to 'mysite.wp_citydb' Just run the code in the SQL tab:

RENAME TABLE citydb TO wp_citydb;

I think we are close to the conclusion :)

spoonyto commented 8 years ago

Hi @

Almost there :)

I have renamed table...but, what I have now

image

And error in log [Wed May 04 23:44:28 2016] [error] [client 188.165.161.192] PHP Warning: Creating default object from empty value in /var/www/ebruart/data/www/ebruworld.com/wp-content/themes/Lucid/epanel/custom_functions.php on line 854

and in that file I have, code below begins from 854

`$theme_request = wp_remote_post( 'http://www.elegantthemes.com/api/api.php', $options ); if ( !is_wp_error($theme_request) && wp_remote_retrieve_response_code($theme_request) == 200 ){ $theme_response = unserialize( wp_remote_retrieve_body( $theme_request ) ); if ( !empty($theme_response) ) { $update_transient->response = array_merge(!empty($update_transient->response) ? $update_transient->response : array(),$theme_response); $last_update->checked = $themes; $last_update->response = $theme_response; } }

$last_update->last_checked = time();
set_site_transient( 'et_update_themes', $last_update );

return $update_transient;

}`

Sorry @fab01 and thank you for you patience! Really appreciate it!

fab01 commented 8 years ago

Hi @spoonyto, normally a warning doesn't affect on the flow since it's just a warning. The problem is for sure related to the other functions get_states or get_cities. Just put an alert directly in the js like this:

function get_states(countryCODE, callback) { var data = { action: 'get_states_call', country_code: countryCODE }; $.post( ajaxurl, data, function(response) { alert(response); }); } or get_states($(this).val(), function(response) { var obj = JSON.parse(response); var len = obj.length; var $stateValues = ''; alert(response); });

anyway wherever you find the 'response' try to play around js to print it out somewhere (alert window is the best option).

Let me know what's the output that u get. F

spoonyto commented 8 years ago

Hi @fab01

I have tried it...but now there is nothing in log.

fab01 commented 8 years ago

Hi @spoonyto,

you don't have anything in the log because it's (99%) an error related to Ajax. If the alert window is "white" it means that the JSON object is empty and you need to investigate around that. Check the ids of the fields, names must be correct and check the correct registration of the php functions as well:

add_action('wp_ajax_get_states_call', 'get_states_call');
add_action('wp_ajax_nopriv_get_states_call', 'get_states_call');

add_action('wp_ajax_get_cities_call', 'get_cities_call');
add_action('wp_ajax_nopriv_get_cities_call', 'get_cities_call');

Check also for the Ajax Url even though I think it's not necessary since you are working on backend environment. Just put in the functions.php the following code [this is normally used to implement ajax url on frontend while on backed should be provided by default]:

function pluginname_ajaxurl() {
?>
<script type="text/javascript">
  var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
<?php
}

add_action('wp_head','pluginname_ajaxurl');
spoonyto commented 8 years ago

Hi @fab01

I have added code to implement ajax url on frontend, then checked functions.php, I mean registration of functions that you mentioned above (all good, but result the same) I copied code to functions.php from your site, I think there all should fine.

What do you believe, is it possible that code of my template could cause this issue?

fab01 commented 8 years ago

Hi @spoonyto, @ this point the reason could be anything even though I would exclude PHP and WP, since the fields are correctly generated. I'm pretty sure the problem is just related to JS. Check very well (also case sensitive) the ids/classes names of the fields. Check also if the JS file code is properly loaded wihin the page. Can you find countryField.js within your html page and related source code?

spoonyto commented 8 years ago

Hi @fab01 I will check. Probably I have not done: "anyway wherever you find the 'response' try to play around js to print it out somewhere (alert window is the best option)."

How can I print out it?

fab01 commented 8 years ago

Hi @spoonyto, to display the JSON object returned by the PHP functions you need to insert an alert(response); within the js function 'get_states' like this:

function get_states(countryCODE, callback) {
var data = {
action: 'get_states_call',
country_code: countryCODE
};
$.post( ajaxurl, data, function(response) {
alert(response);
//comment this line -> callback(response);
});
}

in other words replace callback with alert and try it. If you don't see even the alert it means the js is not properly loaded. Let me know ;)

Beee4life commented 7 years ago

@fab01 I'm lost in the conversation... is your code an addition or replacement for @Vheissu's plugin ? Since I would like it to work within ACF (for only a certain amount of countries) which all have states/provences/cities which should show depending on the value chosen in the previous dropdown.

I'd like to have this function in a front-end acf form to submit posts.